Set-Execution policy on remote computer using Variable - powershell

I have a problem reverting back powershell execution policy on remote computer using variable.
$RestrykcjaNowa = 'Bypass'
$RestrykcjaZastana = Invoke-Command -ComputerName $Komputer -ScriptBlock { Get-ExecutionPolicy }
$RestrykcjaZastana
Invoke-Command -ComputerName $Komputer -ScriptBlock { Set-ExecutionPolicy -ExecutionPolicy $RestrykcjaNowa -Scope LocalMachine -Force } | Out-Null
But I got an error
Cannot bind argument to parameter 'ExecutionPolicy' because it is null
When I replace variable $RestrykcjaNowa with value Bypass in last command it goes smoothly.
I noticed that variable $RestrykcjaZastana is not displayed on the screen when called in 2nd line of the code and is of type int but i can't assign value Bypass to integer variable manually.
What is wrong with my approach?

You have two issues to address:
Execution policies are not strings; they are a separate enumerated type, [Microsoft.PowerShell.ExecutionPolicy]. You will have to assign them as such to your variable:
$RestrykcjaNowa = [Microsoft.PowerShell.ExecutionPolicy]::Bypass
Variables outside of scriptblocks need to be referenced from within the script block with using::
Invoke-Command -ComputerName $Komputer -ScriptBlock { Set-ExecutionPolicy -ExecutionPolicy $using:RestrykcjaNowa -Scope LocalMachine -Force } | Out-Null

Variables are not evaluated when Scriptblock is defined.
However for your case, you may use below code with which you can pass variable to scriptblock as an argument:
$RestrykcjaNowa = 'Bypass'
$Komputer = '<Computername>'
$RestrykcjaZastana = Invoke-Command -ComputerName $Komputer -ScriptBlock { Get-ExecutionPolicy }
$RestrykcjaZastana
Invoke-Command -ComputerName $Komputer -ScriptBlock {param($RestrykcjaNowa) Set-ExecutionPolicy -ExecutionPolicy $RestrykcjaNowa -Scope LocalMachine -Force } -ArgumentList $RestrykcjaNowa | Out-Null

Related

Remotely setting wallpaper on demand via 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]}

Any Idea how to uninstall .MSI with a password in the argument parameter?

I am still pretty new to PowerShell and I am trying to create a PS script that uninstalls a program remotely, however it doesn't seem to be working, it runs fine with no error messages, but just doesn't seem to uninstall the app. I am sure I might be doing something completely wrong... Hoping someone can point me in the right direction. Code below:
$Password = "1234"
Invoke-Command -ComputerName TEST `
-ScriptBlock {
$product = Get-WmiObject win32_product | where{$_.name -eq "Program Name"}
$product.IdentifyingNumber
Start-Process "C:\Windows\System32\msiexec.exe" `
-ArgumentList "/x $($product.IdentifyingNumber) PASSWORD=$Password /quiet /noreboot" -Wait }
Because of the strange indentation in your code I didn't see it at first, but the thing is that variable $Password is unknown in the scriptblock, unless you either scope it with using: or send it as parameter in the ArgumentsList parameter of the scriptblock and add a param() block in there.
Also, as commented, only proceed with the Start-Process command if you have checked that the Get-WmiObject call actually returned a valid product object.
Try
Using the using: scope modifier
$Password = "1234"
Invoke-Command -ComputerName TEST -ScriptBlock {
$product = Get-WmiObject win32_product | Where-Object {$_.name -eq "Program Name"}
if ($product) {
$argList = "/x $($product.IdentifyingNumber) PASSWORD=$using:Password /quiet /noreboot"
Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList $argList -Wait
}
}
or use a param() block in the scriptblock
$Password = "1234"
Invoke-Command -ComputerName TEST -ScriptBlock {
param( $Password )
$product = Get-WmiObject win32_product | Where-Object {$_.name -eq "Program Name"}
if ($product) {
$argList = "/x $($product.IdentifyingNumber) PASSWORD=$Password /quiet /noreboot"
Start-Process "C:\Windows\System32\msiexec.exe" -ArgumentList $argList -Wait
}
} -ArgumentList $Password
You may consider using the newer Get-CimInstance cmdlet instead of Get-WmiObject

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:

Start-Job scriptblock passing of variable

I got a PowerShell script that starts another script and passes parameters to it.
I do this with Start-Job as I do not want to wait until the second script is finished:
ScriptA:
start-job -name EnableAutoUnlock -scriptblock {Invoke-Command -script { C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "\\Path\To\Script\EnableAutoUnlock.ps1" $VolumeDriveLetter }}
ScriptB:
[CmdletBinding()]
Param (
[Parameter(Position=0)]
[string]$drive
)
<do stuff with $drive here>
$VolumeDriveLetter is just a Drive Letter that gets processed i.e. "C:"
Unfortunately the passing of the Parameter by variable does not work although $VolumeDriveLetter has the expected Value but typing it does work correctly.
Works:
start-job -name EnableAutoUnlock -scriptblock {Invoke-Command -script { C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "\\Path\To\Script\EnableAutoUnlock.ps1" C: }}
Does not Work
$VolumeDriveLetter = "C:"
start-job -name EnableAutoUnlock -scriptblock {Invoke-Command -script { C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "\\Path\To\Script\EnableAutoUnlock.ps1" $VolumeDriveLetter }}
EDIT: ScriptB Outputs the passed Variable as empty
What am I missing to get passing of the Variable to work?
You can use the using prefix to access the value within a scriptblock:
$VolumeDriveLetter = "C:"
start-job -name EnableAutoUnlock -scriptblock {Invoke-Command -script { C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "\\Path\To\Script\EnableAutoUnlock.ps1" $using:VolumeDriveLetter }}
Or you use the -ArgumentList parameter and pass the parameter to the scriptblock:
start-job -name EnableAutoUnlock -scriptblock {
Param($VolumeDriveLetter)
Write-Host $VolumeDriveLetter
} -ArgumentList $VolumeDriveLetter

Executing program on remote with Powershell

I'm having trouble executing a program on a remote server. This is my command
$appCommand = "`"c:\SIDApps\{0}\{1}.exe`"" -f $destinationDirectory, $processToKill
Invoke-Command -ComputerName $remoteComputer -Credential $credentials -ScriptBlock { $args[0] } -ArgumentList $appCommand
That prints out "c:\SIDApps\FFMBC_Drone\GCUKTransCodeServiceFFMBCApp.exe" (Including the quotes) in the output window, but does not actually execute the program
If you only output $args[0] in the ScriptBlock it will only display the path not run the command.
Have you tried this ?
$appCommand = "c:\SIDApps\{0}\{1}.exe" -f $destinationDirectory, $processToKill
Invoke-Command -ComputerName $remoteComputer -Credential $credentials -ScriptBlock { Start-Process $args[0] } -ArgumentList $appCommand