Forum Discussion
charlie4872
Sep 04, 2020Brass Contributor
Foreach Loop not working
Hello I am trying to get a foreach loop to work but when I run the script the way it is in the below example it only runs against my local computer and not against the computers in computers.txt. Not...
- Sep 04, 2020Hey Charlie4872,
Seems like you are not using $computer variable, so every time it runs the command it runs for your local computer only.
Have you tried using Invoke-Command with -scriptblock. In your case it will be something like below:-
$computers = get-content "C:\scripts\Bitlocker\computers.txt"
foreach ($computer in $computers) {
Invoke-Command -ComputerName $computer -ScriptBlock {Enable-Bitlocker -MountPoint c: -UsedSpaceOnly -SkipHardwareTest -RecoveryPasswordProtector }
Invoke-Command -ComputerName $computer -ScriptBlock {$RecoveryKeyGUID = (Get-BitLockerVolume -MountPoint $env:SystemDrive).keyprotector | where {$_.Keyprotectortype -eq 'RecoveryPassword'} | Select-Object -ExpandProperty KeyProtectorID |manage-bde.exe -protectors $env:SystemDrive -adbackup -id $RecoveryKeyGUID}}
Assuming that you are running 2 different cmdlets.
Thanks
DeepakRandhawa
Sep 04, 2020Iron Contributor
Hey Charlie4872,
Seems like you are not using $computer variable, so every time it runs the command it runs for your local computer only.
Have you tried using Invoke-Command with -scriptblock. In your case it will be something like below:-
$computers = get-content "C:\scripts\Bitlocker\computers.txt"
foreach ($computer in $computers) {
Invoke-Command -ComputerName $computer -ScriptBlock {Enable-Bitlocker -MountPoint c: -UsedSpaceOnly -SkipHardwareTest -RecoveryPasswordProtector }
Invoke-Command -ComputerName $computer -ScriptBlock {$RecoveryKeyGUID = (Get-BitLockerVolume -MountPoint $env:SystemDrive).keyprotector | where {$_.Keyprotectortype -eq 'RecoveryPassword'} | Select-Object -ExpandProperty KeyProtectorID |manage-bde.exe -protectors $env:SystemDrive -adbackup -id $RecoveryKeyGUID}}
Assuming that you are running 2 different cmdlets.
Thanks
Seems like you are not using $computer variable, so every time it runs the command it runs for your local computer only.
Have you tried using Invoke-Command with -scriptblock. In your case it will be something like below:-
$computers = get-content "C:\scripts\Bitlocker\computers.txt"
foreach ($computer in $computers) {
Invoke-Command -ComputerName $computer -ScriptBlock {Enable-Bitlocker -MountPoint c: -UsedSpaceOnly -SkipHardwareTest -RecoveryPasswordProtector }
Invoke-Command -ComputerName $computer -ScriptBlock {$RecoveryKeyGUID = (Get-BitLockerVolume -MountPoint $env:SystemDrive).keyprotector | where {$_.Keyprotectortype -eq 'RecoveryPassword'} | Select-Object -ExpandProperty KeyProtectorID |manage-bde.exe -protectors $env:SystemDrive -adbackup -id $RecoveryKeyGUID}}
Assuming that you are running 2 different cmdlets.
Thanks
- charlie4872Sep 08, 2020Brass Contributor