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
7
  • @santoshsago what do you mean ? you can save it with an .xml extension Commented Jul 5, 2015 at 15:03
  • I am not at all a PowerShell programmer, and hence I have difficulty in coming up with a code for doing the job. The Task is that I need to run a PS script in a server, that will connect to another server and create an XML file. This XML file will be used by another tool to do some stuff defined inside the nodes. Commented Jul 5, 2015 at 15:06
  • 1
    Have a look to the this answer. Commented Jul 5, 2015 at 15:10
  • Specify $path/$Location as a UNC path to create the file on a remote host. You need write access to the respective share for that. Commented Jul 5, 2015 at 15:26
  • 1
    C$ is the admin share and it does represent the C drive but you need admin privs on that machine in order to access it. Commented Jul 5, 2015 at 18:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.