powershell - How to create an XML file on a remote host? -
the script should connect server , create xml file below content.
<?xml version='1.0'?> <action> <type>update_job</type> <attribute name="job_id" value="331" /> <attribute name="variables"> <map> <entry name="cc_waitapprove_continue" value="true"/> <entry name="cc_approved" value="false"/> </map> </attribute> </action>
update 1: able create xml file in same server using following code, not sure how code connect server , create xml file there:
param( [string] $jobid, [string] $path) $location = $path #"c:\users\sks" $x = @" <?xml version='1.0'?> <action> <type>update_job</type> <attribute name="job_id" value="$jobid" /> <attribute name="variables"> <map> <entry name="cc_waitapprove_continue" value="true"/> <entry name="cc_approved" value="false"/> </map> </attribute> </action> "@ new-item -path $location -name "testing.xml" -itemtype file -value $x
update 2: googled , found can use this. c$
means? c:\
?
$uncserver = "\\10.11.12.124" $uncfullpath = "$uncserver\c$\backup\folder" $username = "anton" $password = "p@ssw0rd" net use $uncserver $password /user:$username new-item -path $uncfullpath -name "testing.xml" -itemtype file -value $x
by default windows hosts provide administrators access root folders of drives via hidden shares. names of so-called administrative shares consist of drive letter followed $
(to make share hidden). unc path of administrative share drive c: on host 10.11.12.124 be
\\10.11.12.124\c$
to access administrative share user must have administrative privileges on remote host.
to connect administrative share explicit credentials can use net.exe
:
$username = '...' $password = '...' $path = '\\10.11.12.124\c$' & net use r: $path $password /user:$username
or (more posh) use new-psdrive
cmdlet:
$username = '...' $password = '...' $path = '\\10.11.12.124\c$' $secpw = convertto-securestring $password -asplaintext -force $cred = new-object management.automation.pscredential ($username, $secpw) new-psdrive -name r -psprovider filesystem -root $path -credential $cred
if want avoid storing password in script can have script read file, or (when running script interactively) can use get-credential
prompt credentials.
in case user doesn't have admin privileges (or don't want use account admin privileges operation, idea) need use existing regular share on remote host, or create one:
net share backup=c:\backup\folder /grant:anton,full
on windows 8/server 2012 can use new-smbshare
cmdlet this:
new-smbshare –name backup –path c:\backup\folder -fullaccess anton
an entirely different approach put xml creation code in scriptblock, , run on remote host via invoke-command
:
$sb = { $xml = @" <?xml version='1.0'?> <action> <type>update_job</type> <attribute name="job_id" value="$jobid" /> <attribute name="variables"> <map> <entry name="cc_waitapprove_continue" value="true"/> <entry name="cc_approved" value="false"/> </map> </attribute> </action> "@ new-item -path $args[0] -name 'testing.xml' -itemtype file -value $xml } $server = '10.11.12.124' $path = 'c:\backup\folder' $username = '...' $password = '...' $secpw = convertto-securestring $password -asplaintext -force $cred = new-object management.automation.pscredential ($username, $secpw) invoke-command -computer $server -scriptblock $sb -argumentlist $path -credential $cred
note work must have psremoting enabled.
Comments
Post a Comment