For couple of days I'm struggling with executing powershell code.
Can you give me some working examples ?
I'm trying enable Remoting, without changing ExecutionPolicy if possible.
Client is on WORKGROUP at this state ... later joindomain
Here is what i tried:
Code: Alles auswählen
ExecWith_remoting powershell
[ExecWith_remoting]
powershellCall('
Enable-PSRemoting -SkipNetworkProfileCheck -Force `r`n # tried without `r`n
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'WIN-DC' -Force
')
Code: Alles auswählen
ExecWith_remoting powershell.exe winst /sysnative
[ExecWith_remoting]
Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'WIN-DC' -Force
Code: Alles auswählen
[WinBatch_remove_adcomputer]
"%System%\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -NoProfile -File "%ScriptPath%\rm_adpc.ps1" /WaitSeconds 20
Code: Alles auswählen
$password = "***" | ConvertTo-SecureString -asPlainText -Force
$username = "OPSI\admin"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
$ComputerName = $env:COMPUTERNAME
$Session = New-PSSession -ComputerName WIN-DC -Credential $credential
$Command = {
$env:CN=$using:ComputerName
function Remove-ADComputerAllDCs {
param(
[Parameter(Mandatory=$true, Position = 0)]
[string]$Computer
)
$DCs = @("WIN-DC")
foreach ($DC in $DCs) {
Write-Host "Removing $($Computer) from $($DC)..."
Remove-ADObject (Get-ADComputer -Identity $Computer -Server $DC) -Recursive -Confirm:$false
}
}
Remove-ADComputerAllDCs $env:CN
}
Invoke-Command -Session $Session -ScriptBlock $Command
Remove-PSSession -Session $Session
I also tried, and it's working:
Code: Alles auswählen
set $policy$ = takeString(0,shellCall('powershell.exe get-execution-policy'))
shellCall('powershell.exe set-execution-policy RemoteSigned')
set $list$ = getOutStreamFromSection ('Execwith_ps powershell.exe winst /sysnative')
shellCall('powershell.exe set-execution-policy '+$policy$)
[Execwith_ps]
trap { write-output $_ ; exit 1 }
Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 'WIN-DC' -Force
exit $LASTEXITCODE
Code: Alles auswählen
; Windows Remote Management (WS-Management)
openkey [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinRM]
set "Start" = REG_DWORD:00000002