📜 ⬆️ ⬇️

turn string into scriptblock

If you use powershell, you might have noticed that you cannot send a string to the remote machine via Invoke-Command

You need to convert the line to script block
$scriptBlock = [Scriptblock]::Create($string) 

example:
 $remoteCommand = @" Import-Module ActiveDirectory New-ADOrganizationalUnit -name "@ $scriptBlock = [Scriptblock]::Create($remoteCommand) Invoke-Command -ComputerName AD01 -ScriptBlock $scriptBlock 


you can even do a function for this
 <# Function to Convert a String into a Script Block #> function ConvertScriptBlock { Param( [Parameter( Mandatory = $true, ParameterSetName = '', ValueFromPipeline = $true)] [string]$string ) $scriptBlock = [scriptblock]::Create($string) return $scriptBlock } 


addition from translator:
commands can be separated by a comma so
 $sb = 'get-process; dir' 

can use so
 #             $sb= '$admin = [ADSI]"WinNT://localhost/admin,User"; $admin.SetPassword( "' + $new_pass + '" )' 

')

Source: https://habr.com/ru/post/278693/


All Articles