Powershell passing arguments in ScriptBlock - powershell

I'm trying to get the last write time on a file from a remote server.
This doesn not work:
$server = "MyServerName"
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$args[0]\hot.war" } -argumentlist $server | select -Property LastWriteTime
This does work:
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\MyServerName\hot.war" } -argumentlist $server | select -Property LastWriteTime
Can anyone help make the first set work?

Be careful with variables in strings: "\\$args[0]\hot.war" will be expanded to \\MyServerName[0]\hot.war.
Use "\\$($args[0])\hot.war" to be sure that $args[0] will be treated as a single expression.
See: http://blogs.msdn.com/b/powershell/archive/2006/07/15/variable-expansion-in-strings-and-herestrings.aspx

Another way, if you are using PowerShell 3. You can do something like this:
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {
Get-ChildItem "\\$using:server\hot.war"
} | select -Property LastWriteTime

You will want to add the server variable into your first line...
$server = "MyServerName"
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$server\hot.war" } -argumentlist $server | select -Property LastWriteTime

Related

Using Powershell Get-ItemProperty through all of AD computers object

I'm a complete newbie in Powershell (and programming as you may have guessed), I want to get the result of the following PS command for each of our AD computer object and print the result in a text file...but I'm completely lost. Does anyone have a lifeline I could hold on to?
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select-String -Pattern "mysoftwarename"
Thank you very much.
$ScriptBlock = {Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select-String -Pattern "mysoftwarename"}
$Computers = (Get-ADComputers -filter * ).name
$Creds = (Get-Credential)
foreach ($Computer in $Computers)
{
"`n`n$Computer`n" >> .\file.txt # "`n" just emulates Enter key press
Invoke-Command -ComputerName $Computer -ScriptBlock $ScriptBlock -Credential $Creds >> .\file.txt
}
This will work fine if you have all your computers online and PS remoting configured properly. Otherwise, it will require modifications.

Invoke-command path null?

I am working on this command to be able to view and edit a registry key remotely to a computer on a joined domain when I need to test something. In this case, I am looking at Excel's "vbawarninsg" key. This works just fine.
cls
$computername = Read-Host "Enter computer name..."
Invoke-Command -ComputerName $computername {Get-ItemProperty -Path 'REGISTRY::HKEY_USERS\xxxxxxx\Software\Policies\Microsoft\office\16.0\excel\security' } |
Select-Object PSComputerName, vbawarnings, PSParentPath | fl
$name = "vbawarnings"
The next part is to set a new value for the "vbawarnings" key using New-ItemProperty. When I assigned a variable for the -Path name it gives me an error "Cannot bind argument to parameter 'Path' because it is null."
This is the script that gives me an error
cls
$computername = Read-Host "Enter computer name..."
$registryPath = 'REGISTRY::HKEY_USERS\xxxxxxx\Software\Policies\Microsoft\office\16.0\excel\security'
Invoke-Command -ComputerName $computername {Get-ItemProperty -Path $registryPath } |
Select-Object PSComputerName, vbawarnings, PSParentPath | fl
$name = "vbawarnings"
$value = Read-Host "To modify...Enter a value"
New-ItemProperty -Path $registryPath -Name $name -Value $value `
-PropertyType DWORD -Force -Verbose | Out-Null
Any help is greatly appreciated!
In order to use a variable remotely (such as the case with Invoke-Command), you need to use the $using: variable scope:
Invoke-Command -ComputerName $cn {
Get-ItemProperty -Path $using:regPath
}
or pass it as a parameter:
Invoke-Command -ComputerName $cn {
param($path)
Get-ItemProperty -Path $path
} -ArgumentList '-path', $regPath
See this article
When you do Invoke-Command, that scriptblock gets sent to the remote server.
Invoke-Command -ComputerName $computername {Get-ItemProperty -Path $registryPath }
On the remote server, $registryPath is null even though you have it locally in your script.
So just hardcode the registry path instead:
Invoke-Command -ComputerName $computername {Get-ItemProperty -Path 'REGISTRY::HKEY_USERS\xxxxxxx\Software\Policies\Microsoft\office\16.0\excel\security' }

Powershell list with IPAddress, Prefixlength and Computernames

I've write a PS Script, that gives me all IP Addresses and Prefixlength of Servers from a text file. See code:
$cred = Get-Credential
$computers = Get-Content -Path C:\Users\XX_YY\Desktop\test_hostname_file.txt
invoke-command -computername $computers -Credential $cred -scriptblock {get-netipaddress -AddressFamily IPv4 } | where {$_.ipaddress -like "10.*"} | ft -AutoSize IPaddress, prefixlength
This Script gives me the result as a table. But I need the Computernames as well in the table.
have you an idea how I can handle this?
Cheers Sam
Well, I would do it in another way, but to stick to your question:
You can use the Automatic variable $ENV:ComputerName to get the computer name and add it to the output using a PS Object
$cred = Get-Credential
$computers = Get-Content -Path C:\Users\XX_YY\Desktop\test_hostname_file.txt
invoke-command -computername $computers -Credential $cred -scriptblock {
$IP = get-netipaddress -AddressFamily IPv4 | where {$_.ipaddress -like "10.*"}
$Row = "" | Select Computer,IPaddress,prefixlength
$Row.Computer = $env:COMPUTERNAME
$Row.IPaddress = $IP.IPaddress
$Row.prefixlength = $IP.prefixlength
return $row
}

Stopping & Restarting Services Remotely Using Set-Service

I've got a list of 10-15 services that I routinely need to restart on 6 servers. I have a script that calls a list of services, then calls a list of the servers, and then stops all the services:
$Services = Get-Content -Path "C:\Powershell\Services.txt"
$Machines = Get-Content -Path "C:\Powershell\Machines.txt"
Get-Service -Name $Services -ComputerName $Machines | Set-Service -Status Stopped
I then have another separate script to start them up again:
$Services = Get-Content -Path "C:\Powershell\Services.txt"
$Machines = Get-Content -Path "C:\Powershell\Machines.txt"
Get-Service -Name $Services -ComputerName $Machines | Set-Service -Status Running
I've checked around and can't seem to find a way of putting this into a single script. As I understand, Set-Service only has the ability to Stop, Start & Pause services, not restart them at the same time.
Any ideas? I might be missing something completely obvious.
To restart services simply use Restart-Service:
$Services = Get-Content -Path "C:\Powershell\Services.txt"
$Machines = Get-Content -Path "C:\Powershell\Machines.txt"
Get-Service -Name $Services -ComputerName $Machines | Restart-Service
Since according to the comments PowerShell v6 has removed support for remote access from the *-Service cmdlets you need to resort to Invoke-Command for remote execution when running v6 or newer, like this:
Invoke-Command -Computer $Machines -ScriptBlock {
Get-Service -Name $using:Services -ErrorAction SilentlyContinue |
Restart-Service
}
or like this:
Invoke-Command -Computer $Machines -ScriptBlock {
Restart-Service $using:Services -ErrorAction SilentlyContinue
}
Another option would be WMI:
$fltr = ($Services | ForEach-Object { 'Name="{0}"' -f $_ }) -join ' or '
Get-WmiObject Win32_Service -Computer $Machines -Filter $fltr | ForEach-Object {
$_.StopService()
$_.StartService()
}
I am with Ansgar, this should work
$Services = Get-Content -Path "C:\Powershell\Services.txt"
$Machines = Get-Content -Path "C:\Powershell\Machines.txt"
foreach ($service in $services){
foreach ($computer in $Machines){
Invoke-Command -ComputerName $computer -ScriptBlock{
Restart-Service -DisplayName $service}
}
}
it is a little messy but should give you a starting point
Sorry I forgot to take time to explain what is going on, so you import each of your txt docs and then it will process for each service and each computer and restart the services.
You can try this single liner command:
Get-Content .\services.txt | %{Get-WmiObject -Class Win32_Service -ComputerName (Get-Content .\computers.txt) -Filter "Name='$_'"} | %{$_.StopService()}; Get-Content .\services.txt | %{Get-WmiObject -Class Win32_Service -ComputerName (Get-Content .\computers.txt) -Filter "Name='$_'"} | %{$_.StartService()}

PowerShell: string variable ignored by Get-ChildItem

Below PowerShell script should measure folder sizes on remote computers but apparently my $Desktop variable value is somehow ignored by Get-ChildItem and I get 0.00 MB. But when I replace $Desktop with explicit string i.e. "C:\Users\user1\Desktop" it works alright and I get e.g. 10.MB. Am I doing something wrong?
$file1="C:\computers_users.csv"
import-csv $file1 | ForEach-Object{
$Desktop = "C:\Users\$($_.user)\Desktop"
Invoke-Command -ComputerName $_.computer -ScriptBlock {
$FldSize =(Get-ChildItem $Desktop -recurse | Measure-Object -property length -sum)
"{0:N2}" -f ($FldSize.sum / 1MB) + " MB"}
}
try this:
Invoke-Command -ComputerName $_.computer -ScriptBlock {
$FldSize =(Get-ChildItem $args[0] -recurse | Measure-Object -property length -sum)
"{0:N2}" -f ($FldSize.sum / 1MB) + " MB"} -argumentlist $desktop
You need to pass the argument with -argumentlist because the invoke-command create a new powershell session not aware of calling session variables.
On a side note, in PowerShell 3.0 you'll be can use $using to pass local variables and to the remote machine:
Invoke-Command ... -ScriptBlock { $FldSize =(Get-ChildItem $using:Desktop ...