1

The script should connect to another server and create a XML file with the 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: I was able to create a XML file in the same server using the following code, I am not sure how to code to connect to another server and create the 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: I googled and found I can use like this. What does the C$ means? Is it 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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

1 Answers1

0

By default Windows hosts provide administrators with access to the root folders of all drives via hidden shares. The names of the so-called administrative shares consist of the drive letter followed by a $ (to make the share hidden). The UNC path of the administrative share for drive C: on host 10.11.12.124 would be

\\10.11.12.124\C$

To access an administrative share the user must have administrative privileges on the remote host.

To connect an administrative share with explicit credentials you can use net.exe:

$username = '...'
$password = '...'
$path     = '\\10.11.12.124\C$'

& net use R: $path $password /user:$username

or (more PoSh) use the 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 you want to avoid storing the password in the script you can have the script read it from a file, or (when running the script interactively) you can use Get-Credential to prompt for credentials.

In case your user doesn't have admin privileges (or you don't want to use an account with admin privileges for the operation, which is usually a good idea) you need to use an existing regular share on the remote host, or create one:

net share backup=C:\backup\folder /grant:anton,full

On Windows 8/Server 2012 you can also use the New-SmbShare cmdlet for this:

New-SmbShare –Name backup –Path C:\backup\folder -FullAccess anton

An entirely different approach would be to put your XML creation code in a scriptblock, and run that on the 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 that for this to work you must have PSRemoting enabled.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328