I need help to to execute a command in loop for multiple servers, I have these command I want to run them in a loop for multiple servers<\/p>\n

Advertisement

Invoke-Command -Computer ‘IP’ -ScriptBlock {Remove-Item -Path $args[0] -force } -ArgumentList 'c:\\Folder'<\/p>\n

Advertisement

$Session = New-PSSession -ComputerName “IP” -Credential “user-name”
\nCopy-Item \"C:\\Folder\" -Destination \"C:\" -ToSession $Session<\/p>\n

Also how do I pass the password for the copy command ?
\nAny help is appreciated.<\/p>","upvoteCount":2,"answerCount":38,"datePublished":"2021-02-22T15:13:55.000Z","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"},"acceptedAnswer":{"@type":"Answer","text":"

you’d use invoke -command instead and a foreach loop<\/p>\n

\n
\n
<\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
\n

Also how do I pass the password for the copy command ?<\/p>\n<\/blockquote>\n<\/aside>\n

hm?<\/p>\n

e.g.<\/p>\n

$cred = (Get-Credential)\n$list = get-content \"list.txt\"\n\nforeach($machine in $list){\n    invoke-command -ComputerName $machine -ScriptBlock {\n        Copy-Item \"C:\\Folder\\\" -Destination \"C:\\\"\n    } -Credential $cred\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T15:21:25.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"

I need help to to execute a command in loop for multiple servers, I have these command I want to run them in a loop for multiple servers<\/p>\n

Invoke-Command -Computer ‘IP’ -ScriptBlock {Remove-Item -Path $args[0] -force } -ArgumentList 'c:\\Folder'<\/p>\n

$Session = New-PSSession -ComputerName “IP” -Credential “user-name”
\nCopy-Item \"C:\\Folder\" -Destination \"C:\" -ToSession $Session<\/p>\n

Also how do I pass the password for the copy command ?
\nAny help is appreciated.<\/p>","upvoteCount":2,"datePublished":"2021-02-22T15:13:55.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/1","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"

Tried this<\/p>\n

$computers = Get-Content C:\\computers.txt<\/p>\n

foreach ($computer in $computers)
\n{
\nInvoke-Command -Computer ‘$computers’ -ScriptBlock {Remove-Item -Path $args[0] -force } -ArgumentList 'c:\\Folder'<\/p>\n

}<\/p>\n

but getting error “Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.”<\/p>\n

I have added IPs in file computer.txt<\/p>\n

what am I doing wrong here ?<\/p>","upvoteCount":0,"datePublished":"2021-02-22T15:55:53.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/3","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
\n

Tried this<\/p>\n

$computers = Get-Content C:\\computers.txt<\/p>\n

foreach ($computer in $computers)
\n{
\nInvoke-Command -Computer ‘$computers’ -ScriptBlock {Remove-Item -Path $args[0] -force } -ArgumentList 'c:\\Folder'<\/p>\n

}<\/p>\n

but getting error “Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.”<\/p>\n

I have added IPs in file computer.txt<\/p>\n

what am I doing wrong here ?<\/p>\n<\/blockquote>\n<\/aside>\n

From the Invoke-Command documentation:<\/p>\n

To use an IP address in the value of ComputerName, the command must include the Credential parameter. The computer must be configured for the HTTPS transport or the IP address of the remote computer must be included in the local computer's WinRM TrustedHosts list. For instructions to add a computer name to the TrustedHosts list, see How to Add a Computer to the Trusted Host List.\n<\/code><\/pre>\n

Verify you meet those conditions or just use the FQDN or NETBIOS names.<\/p>","upvoteCount":2,"datePublished":"2021-02-22T16:04:58.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/4","author":{"@type":"Person","name":"tulioarends","url":"https://community.spiceworks.com/u/tulioarends"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
\n

I have added IPs in file computer.txt<\/p>\n<\/blockquote>\n<\/aside>\n

in what format?<\/p>\n

like so?<\/p>\n

192.168.15.20\n192.168.15.21\n192.168.15.22\n<\/code><\/pre>\n
\n
\n
<\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
\n

what am I doing wrong here ?<\/p>\n<\/blockquote>\n<\/aside>\n

    \n
  • you can not use single quotes<\/li>\n<\/ul>\n

    Why are you trying to feed it “C:\\Folder” as argument?
    \ndid you look at the sample I posted?
    \nit should be also computer and not computerS in a foreach<\/p>\n

     Invoke-Command -Computer '$computers' \n# needs to be \n Invoke-Command -Computer $computer\n#or\n Invoke-Command -Computer \"$computer\"\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T16:05:16.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    Also…
    \nIf you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>

    \n
    \n
    <\/div>\n \"\"\n PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> Programming & Development<\/span><\/span><\/a>\n <\/div>\n
    \n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n

    \"codebutton_small.png\"<\/p>","upvoteCount":0,"datePublished":"2021-02-22T16:05:51.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    $computers = Get-Content C:\\computers.txt\n\nforeach ($computer in $computers)\n{\n  Invoke-Command -Computer '$computers' -ScriptBlock {Remove-Item -Path $args[0] -force } -ArgumentList 'c:\\Folder\\'\n\n}\n<\/code><\/pre>\n

    try like so
    \n(again, we can not validate your input / computers.txt file, it should be just each IP listed under each other, no blank line )<\/p>\n

    $computers = Get-Content C:\\computers.txt\n\nforeach ($computer in $computers){\n    Invoke-Command -Computer $computer -ScriptBlock {\n        Remove-Item \"c:\\Folder\" -force\n    }\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T16:08:26.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
    \n
    \n
    <\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
    \n

    I have added IPs in file computer.txt<\/p>\n

    what am I doing wrong here ?<\/p>\n<\/blockquote>\n<\/aside>\n

    This is the problem. Invoke-Command uses WinRM for remoting, and WinRM uses DNS names by default. If you want to use IP address, the IP has to be added to the trusted list.<\/p>\n

    To use an IP address in the value of ComputerName, the command must include the Credential parameter. The computer must be configured for the HTTPS transport or the IP address of the remote computer must be included in the local computer's WinRM TrustedHosts list.\n<\/code><\/pre>\n
    \n
    \n\n learn.microsoft.com<\/a>\n <\/header>\n\n
    \n \n\n

    about Remote Troubleshooting - PowerShell<\/a><\/h3>\n\n

    Describes how to troubleshoot remote operations in PowerShell.<\/p>\n\n\n <\/article>\n\n

    \n \n \n <\/div>\n\n
    <\/div>\n<\/aside>\n","upvoteCount":1,"datePublished":"2021-02-22T16:09:11.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/8","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

    @alexw<\/a> Thank you so much for your help, I was able to run it with first code you gave​.<\/p>\n

    Thanks again for your time I really appreciate your help.<\/p>\n

    $cred = (Get-Credential)\n$list = get-content \"computers.txt\"\n\nforeach($machine in $list){\n    invoke-command -ComputerName $machine -ScriptBlock {\n        Invoke-Expression -Command:\"cmd.exe /c 'C:\\batch\\task.bat'\"\n    } -Credential $cred\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2021-02-22T16:17:30.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/9","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"

    @alexw<\/a> ​ Need one more help. I am trying to copy a folder to remote servers , is this the right command to do the same ?<\/p>\n

    $cred = (Get-Credential)\n$list = get-content \"computers.txt\"\n\nforeach($machine in $list){\n    invoke-command -ComputerName $machine -ScriptBlock {\n        Copy-Item \"C:\\Folder\\\" -Destination \"C:\\\" -Recurse\n    } -Credential $cred\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T16:25:01.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/10","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"
    \n
    \n
    <\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
    \n

    I am trying to copy a folder to remote servers<\/p>\n<\/blockquote>\n<\/aside>\n

    So you want FROM ComputerA to tell ComputerB to copy something to ComputerC ?<\/p>\n

    That’s the double hop problem, why don’t you copy it directly?<\/p>\n

    Read more here:<\/p>\n

    \n
    \n\n learn.microsoft.com<\/a>\n <\/header>\n\n
    \n \n\n

    Making the second hop in PowerShell Remoting - PowerShell<\/a><\/h3>\n\n

    This article explains the various methods for configuring second-hop authentication for PowerShell remoting, including the security implications and recommendations.<\/p>\n\n\n <\/article>\n\n

    \n \n \n <\/div>\n\n
    <\/div>\n<\/aside>\n
    \n
    \n \n\n Progress Blogs – 15 Aug 18<\/a>\n <\/header>\n\n
    \n
    <\/div>\n\n

    The Infamous Double-Hop Problem in PowerShell<\/a><\/h3>\n\n

    In this article, we will discuss how to get around the infamous double-hop problem in PowerShell.<\/p>\n\n\n <\/article>\n\n

    \n \n \n <\/div>\n\n
    <\/div>\n<\/aside>\n

    https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp/<\/a><\/p>","upvoteCount":0,"datePublished":"2021-02-22T16:27:34.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/11","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    I just want to copy from Computer A to multiple computers (Computer B,C,D) I used the below command but was unable to put it in a loop<\/p>\n

    $Session = New-PSSession -ComputerName \"IP\" -Credential \"user-name\"\nCopy-Item \"C:\\Folder\\\" -Destination \"C:\\\" -ToSession $Session -Recurse\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T16:31:46.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/12","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"
    \n
    \n
    <\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
    \n

    I just want to copy from Computer A to multiple computers (Computer B,C,D) I used the below command but was unable to put it in a loop<\/p>\n

    $Session = New-PSSession -ComputerName \"IP\" -Credential \"user-name\"\nCopy-Item \"C:\\Folder\\\" -Destination \"C:\\\" -ToSession $Session -Recurse\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\n

    You obviously can not use \"C:\" for that. you need a share or a file where everyone can access it.<\/p>\n

    So you can either put it in a share, or use the admin share.<\/p>\n

    #admin share\nCopy-Item \"\\\\computerA\\C$\\Folder\" -Destination \"C:\\\"\n# shared folder\nCopy-Item \"\\\\computerA\\ShareName\\Folder\" -Destination \"C:\\\"\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T16:34:11.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/13","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    @alexw<\/a> ​ got that to work used the below code. Thanks for all your help<\/p>\n

    $cred = (Get-Credential)\n$list = get-content \"computers.txt\"\n\nforeach($machine in $list){\n    $Session = New-PSSession -ComputerName \"$machine\" \nCopy-Item \"C:\\Folder\\\" -Destination \"C:\\\" -ToSession $Session -Recurse\n    } -Credential $cred\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T16:41:36.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/14","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"

    @alexw<\/a> ​ When trying to delete folder on remote server I have to say Yes for each machine when i run the script<\/p>\n

    I tried -Force , Confirm:$fasle but nothing works<\/p>\n

    Any thoughts how it can be done?<\/p>\n

    $cred = (Get-Credential)\n$list = get-content \"computers.txt\"\n\nforeach($machine in $list){\n    invoke-command -ComputerName $machine -ScriptBlock {\n        Remove-Item -Path $args[0] -force } -ArgumentList 'c:\\Folder\\'\n    } -Credential $cred\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T18:02:49.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/15","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"
    \n
    \n
    <\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
    \n

    Neally​ When trying to delete folder on remote server I have to say Yes for each machine when i run the script<\/p>\n

    I tried -Force , Confirm:$fasle but nothing works<\/p>\n<\/blockquote>\n<\/aside>\n

    that should do the trick, did you typo it?<\/p>\n

    Remove-Item -Path $args[0] -force -confirm:$false -recurse\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T18:05:27.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/16","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    @alexw<\/a> ​ You are the man. Thanks a ton.<\/p>","upvoteCount":0,"datePublished":"2021-02-22T18:07:17.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/17","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"

    I am getting error for WinRm on a lot of the servers. How can I enable it on the servers remotely?
    \nI don’t have AD<\/p>","upvoteCount":0,"datePublished":"2021-02-22T20:32:28.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/18","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}},{"@type":"Answer","text":"

    \n
    \n
    <\/div>\n\"\" spiceuser-3fkxp:<\/div>\n
    \n

    I am getting error for WinRm on a lot of the servers. How can I enable it on the servers remotely?
    \nI don’t have AD<\/p>\n<\/blockquote>\n<\/aside>\n

    How do you access them currently? RMM software? psexec?<\/p>\n

    e.g.<\/p>\n

    psexec.exe \\\\$computer -s C:\\Windows\\System32\\winrm.cmd qc -quiet\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-02-22T20:34:23.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/19","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

    @alexw<\/a> using rmm
    \ngetting this error<\/p>\n

      \n
    • CategoryInfo : OpenError: (192.168.1.10:String) <\/span>, PSRemotingTransportException<\/li>\n
    • FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken​<\/li>\n<\/ul>","upvoteCount":0,"datePublished":"2021-02-25T14:25:33.000Z","url":"https://community.spiceworks.com/t/loop-in-powershell/791504/20","author":{"@type":"Person","name":"spiceuser-3fkxp","url":"https://community.spiceworks.com/u/spiceuser-3fkxp"}}]}}