Remotely setting wallpaper on demand via powershell - powershell

I have created a function Set-Wallpaper:
Function Set-WallPaper($Value)
{
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
rundll32.exe user32.dll, UpdatePerUserSystemParameters
}
and can run it locally to use a network path to the desired jpg
Set-Wallpaper -value "\\server\share\image.jpg"
and it works.
Now I want to run this function on a remote PC, I tried
Invoke-Command -ComputerName TARGETPC Set-Wallpaper -value "\\server\share\image.jpg"
but it errors out with
Invoke-Command : A parameter cannot be found that matches parameter name 'value'.
what am I missing?

$session= new-pssession -Computername "yourClientname"
$reslt= Invoke-Command -Session $session -ScriptBlock {your code}
If u want to ahnd over an argument use it like this
$reslt= Invoke-Command -Session $session -argumentlist $argument -ScriptBlock {param ($argument) your code with $argument[0]}

Related

Trying to verify that a remote server can access CIF share

I am trying to verify that a remote server has access to a CIF share
$RemoteMoteMachineName = 'ServerName'
$ShareToAccess='\\Share\Level1Folder'
$RemoteSession = New-PSSession -ComputerName $RemoteMachineName
$RemoteTest = Invoke-Command -Session $RemoteSession -ScriptBlock {Test-Path -PATH -ArgumentList $using:ShareToAccess}
If I run the above commands manually with hard coded values, it works.
If I try to use the variables I receive
Test-Path: A positional parameter cannot be found that accepts argument '\Share\Level1\Folder'
Ideas?
Resolved, used instead (create a fake drive and remove if successful:
$RemoteTest = Invoke-Command -Session $RemoteSession -ScriptBlock {New-PSDrive -Name TestName -PSProvider FileSystem -Root $using:ShareToAccess}
If ($RemoteTest){
Write-Host "Remote test pass $RemoteMachine to $ShareToAccess"
Invoke-Command -Session $RemoteSession -ScriptBlock {Remove-PSDrive -Name TestName }
Else {Write-Host "Share access Fail"}

Passing parameter using variable While generating DSC configuration

I am executing Powershell DSC script from Powershell. Below is the code snippet
Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
param ($driveformat)
cd c:/provisioning
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Install-Module -Name PSDscResources -Force
Install-Module -Name xStorage -Force
. .\DiskSetup.ps1
disksconfig -outputpath C:\DataDiskSetting -driveFormat $driveFormat
Start-DscConfiguration -Path C:\DataDiskSetting -Wait -Force -Verbose
} -ArgumentList ($driveformat)
While generating configuration I want to pass driveformat as variable "$driveFormat" instead of hardcoding like "NTFS". Somehow its not getting value of $driveformat any idea how we can solve this.
You can add a named Parameter $driveformat in your script. See below example:
Param([String]$driveformat)
Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
param ($driveformat)
...
} -ArgumentList ($driveformat)
Then in the powershell task from pipeline, add -driveformat "NTFS" in the argument field. See below screenshot: (I defined a pipeline variable driveformat to hold the value "NTFS")
Or, you can add an Argument (eg. $driveformat = $args[0]) in your scripts. See below:
$driveformat = $args[0]
Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
param ($driveformat)
...
} -ArgumentList ($driveformat)
Then you can directly pass the variable ("NTFS") in the Arguments field of powershell task:

Calling param object inside script block

I am trying to create a folder remotely but the parameter that I type in command line doesn't pass in script block. Below code will only create directory webapp.
$computerName = "s3apdev0074"
Invoke-Command -ComputerName $computerName -ScriptBlock { param([string]$appname) md d:\webapp\$appname}
Please try this:
$ComputerName = 's3apdev0074'
$AppName = Read-Host "Please enter the application name"
Invoke-Command -ComputerName $ComputerName -ArgumentList $AppName -ScriptBlock {
Param (
[String]$AppName
)
# DOS:
md d:\webapp\$AppName
# PowerShell: New-Item -Path "d:\webapp\$AppName" -ItemType Directory
}
It seem you are only missing the -ArgumentList parameter to pass in your local variable.
More info here:
Get-Help Invoke-Command -Parameter ArgumentList

PSSession will not change the working directory

I have a script I'm trying to implement. If I run each line seperately, it works just fine. But if I place it in a function, or run with PowerGUI or Powershell ISE, it errors out. The problem is the script isn't changing the working directory, therefore the file that Invoke-Command is calling isn't found.
$DLUpdate = New-PSSession -ComputerName msmsgex10wprd03 -Credential $DLUpdateCred -Name DLUpdate
Enter-PSSession -Session $DLUpdate
$UpdateDLPath = 'c:\users\mascott2\Desktop\Distrolist\Updates\'
Set-Location $UpdateDLPath
Invoke-Command -ScriptBlock {cmd.exe "/c updatedls.bat"}
Exit-PSSession
Remove-PSSession -Name DLUpdate
You shouldn't be using Enter-PSSession in a script like that. Put all the commands you want into the scriptblock you use with Invoke-Command and run it against your session:
$DLUpdate = New-PSSession -ComputerName msmsgex10wprd03 -Credential $DLUpdateCred -Name DLUpdate
Invoke-Command -Session $DLUPdate -ScriptBlock {
$UpdateDLPath = 'c:\users\mascott2\Desktop\Distrolist\Updates\'
Set-Location $UpdateDLPath
cmd.exe "/c updatedls.bat"
}
Remove-PSSession -Name DLUpdate

invoke-command doesn't work as expected

I'm trying to use invoke-command to find a specific process using this code
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {Get-Process -Name "winlogon" | where{$_.Id -like $args[0]} }
This command doesn't work, but if I use the numeric value contained in
$selected_server.ProcessId that is 8900, instead of using $args[0], it works.
I also tried to execute this command to verify if variables are read correctly and it seems so
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {$args[0]; $args[0].gettype().fullname}
> 8900
> System.Int32
Am I missing something?
Don't know why but this works ( maybe $args in foreach-object scriptblock is out of scope):
Invoke-Command -ComputerName $selected_server.ServerName `
-ArgumentList $selected_server.ProcessId -ScriptBlock `
{param ($x) Get-Process -Name "winlogon" | where{$_.Id -like $x} }
C.B's answer is good & works anywhere you have remoting available (v2.0 & higher), but there is another (easier) way if you're using PowerShell 3.0 - the Using scope modifier. See about_Remote_Variables
Invoke-Command -ComputerName $selected_server.ServerName -ArgumentList $selected_server.ProcessId -ScriptBlock {Get-Process -Name "winlogon" | where{$_.Id -like $Using:selected_server.ProcessId} }