I have the following simple script which I'm using to push some files to a number of Win7 and Server2008 machines. In most cases the local user has no password. However the scripts fails because I'm trying to write through the admin share (c$). What is the most simple way to pass a user for each host machine in the ip.txt?

Also if I'm on a domain how simple is it to modify the script to authenticate with a user and password.

 Write-Host 'Deploy Tools'

    $reader = [System.IO.File]::OpenText("c:\scripts\ip.txt")
    try {
        for(;;) {
            $line = $reader.ReadLine()
            if ($line -eq $null) { break }  
                echo "**** files being copied to: $line ****"
                xcopy c:\scripts\tools  \\$line\c$\windows\system32\ /Y
    finally {

Since you are already calling a non-PowerShell command (xcopy), you could as well call another one:

net use \\$server\c$ /user:$username $password
xcopy c:\scripts\tools  \\$server\c$\windows\system32\ /Y

As for storing usernames and passwords in your input file, you can do that quite easily by using the CSV format and the Import-Csv cmdlet.

Also, see this StackOverflow question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.