Let imagine there is a PowerShell script already there in a remote computer and let that script composed of 8 lines. When I try to run that script from my local computer by using “Invoke-Command”, all execution were display in my local computer’s PowerShell ISE console from start to end. Let, due to any reason if it halt at line number 4, it did not continue to execute remaining lines independently to the end in that remote computer. How can I run that independently non-interactively without showing each line output in local computer PowerShell ISE console ? Because the results are returned to my local computer.
It may help when you actually show your code/script. And - if it’s not obvious from your code - you may explain what you actually want to achieve.
Usually we use Invoke-Command
to run local code on a remote computer.
Set-Location C:\
Clear-Host
$srv = Import-Csv "C:\Users\PSscripts\Servers.csv"
$secpass = ConvertTo-SecureString 'xxxxxxx' -AsPlainText -Force
$MyCred = New-Object System.Management.Automation.PSCredential("domain\user",$secpass)
foreach($object in $srv) {
$Server = $object.HostName
$NWIP = $object.NewIP
$NWGW = $object.NewGW
$OdIP = $object.OldIP
$Fil = "C:\Users\PSscripts\reip.ps1"
if(Get-ChildItem -Path "C:\Users\PSscripts\" -Filter reip.ps1)
{
Remove-Item -Path "C:\Users\PSscripts\reip.ps1"
}
New-Item "C:\Users\\\PSscripts\reip.ps1" -Value "#This file replace old ip with new ip."
Add-Content -Path $Fil -Value "`n`$ip = Get-NetIPAddress -AddressFamily IPv4 | Where-Object IPAddress -NotMatch '169\.254\.|127\.0\.0\.1' | select IPAddress"
Add-Content -Path $Fil -Value "`$ip.IPAddress"
Add-Content -Path $Fil -Value "`$AdptrNam = (Get-NetAdapter).Name"
Add-Content -Path $Fil -Value "Remove-NetRoute"
Add-Content -Path $Fil -Value "New-NetIPAddress -AddressFamily IPv4 -InterfaceAlias `$AdptrNam -IPAddress $NWIP -PrefixLength 23 -DefaultGateway $NWGW"
Add-Content -Path $Fil -Value "Remove-NetIPAddress -IPAddress $OdIP"
$ssn = New-PSSession -ComputerName $Server -Credential $MyCred
Copy-Item "C:\Users\PSscripts\reip.ps1" -Destination "C:\temp" -ToSession $ssn
Invoke-Command -Session $ssn {
Invoke-Expression -Command "C:\temp\reip.ps1"
}
Get-PSSession | Remove-PSSession
}`Preformatted text`
please format all your code as a code block using the “preformatted text” button
Hope you can see the complete script after using the “preformatted text” button
ok now can you try to explain your problem again? You said you might encounter a problem at line 4? Is there an error message you’re getting or a specific issue you’re having? I can’t quite follow what the issue is from your original post.
This is a script I have written to change IP address of some bulk of computers. First it create an another script in local computer. then it copy that script to remote computer, then it trigger that script using Invoke-Command. It successfully changed the IP address and subnet mask in the remote computer. But it got paused when reached to the line to change gateway and removing old IP address. And not go further to execute those steps. That remote script run successfully when executing locally on that remote computer. No syntax error, no bug. All execution steps are showing interactively to my local computer’s PowerShell ISE console one after another, not run independently from start to end.
sounds like maybe you need to add in some logging. maybe just Start/Stop-Transcript at the very least but possibly a function to write out to a file. Maybe some try/catch blocks to try to figure out where in the script it’s stopping.
Is it possible that the Remove-NetIpAddress cmdlet requires confirmation that’s not being satisfied?
Hmmm … just to get this right … you change the IP address and subnet mask of the connection you’re using to run this code on the remote computer, right? … isn’t that like sawing off the branch you are sitting on?
This is correct. Of course you are going to lose connection, you changed the IP of the host you’re connected to. There are so many things wrong with your approach, it’s extremely unnecessary to create a script, copy it to the server, and execute with Invoke-Expression. You can execute the command in a disconnected state and then later retrieve the results from the session.
$srv = Import-Csv "C:\Users\PSscripts\Servers.csv"
$secpass = ConvertTo-SecureString 'xxxxxxx' -AsPlainText -Force
$MyCred = New-Object System.Management.Automation.PSCredential("domain\user",$secpass)
foreach($object in $srv) {
$Server = $object.HostName
$NWIP = $object.NewIP
$NWGW = $object.NewGW
$OdIP = $object.OldIP
$session = Invoke-Command -ComputerName $Server {
$ip = Get-NetIPAddress -AddressFamily IPv4 | Where-Object IPAddress -NotMatch '169\.254\.|127\.0\.0\.1' | select IPAddress
$AdptrNam = (Get-NetAdapter).Name
Write-Host "Removing NetRoute"
Remove-NetRoute
Write-Host "Adding IP $($using:NWIP) to adapter '$AdptrNam'"
New-NetIPAddress -AddressFamily IPv4 -InterfaceAlias $AdptrNam -IPAddress $using:NWIP -PrefixLength 23 -DefaultGateway $using:NWGW
Write-Host "Removing IP $($using:OdIP) from adapter '$AdptrNam'."
Remove-NetIPAddress -IPAddress $using:OdIP
} -Credential $MyCred -InDisconnectedSession
# This will not work until the calling system can resolve the hostname to the new IP!!! This will only remove the session if the Receive-PSSession command succeeds.
try{
$session | Receive-PSSession -ErrorAction Stop
$session | Remove-PSSession
}
catch{
Write-Warning "Error receiving PSSession: $($_.exception.message)"
}
}