如何让 foreach 创建多个变量?
How do you make a foreach create multiple variables?
我正在尝试制作一个脚本来检查 bitlocker 加密百分比并将其设置为一个变量,但对于每个驱动器。
例如。我 运行 我的脚本将在 2 个驱动器上开始 bitlocker 加密。我需要一个 foreach 语句来创建一个变量 foreach 驱动器 (2),然后我将有一个 while 语句表示当驱动器小于 100% 时,重新检查百分比。一旦达到 100%,它将锁定驱动器。
我不知道如何编写代码以便让它为每个检测到并开始加密的驱动器创建一个 while 语句。
对不起,如果这让我感到困惑,我是新手。
这是我到目前为止的代码。 (不要 运行 在你的电脑上,因为它会位锁你的驱动器,哈哈,如果你想 运行 它删除“Enable-Bitlocker”代码)
#Finds all drives connected to the computer, I'm not sure if this will also detect CDs though, but if it does let me know since they can't be bitlocked
$Drives = Get-BitLockerVolume | Where-Object -Property "MountPoint" | Select-Object -ExpandProperty "MountPoint"
#Encrypts the password so it can't be displayed with a read-host command
$SecureString = ConvertTo-SecureString "test" -AsPlainText -Force
#This will run the bitlocker command and encrypt each drive with the secure password.
foreach ($Drive in $Drives){
Enable-BitLocker -MountPoint $Drive -EncryptionMethod Aes256 -Password $SecureString -PasswordProtector -SkipHardwareTest -WhatIf #WhatIf added for safety measures
}
#this is supposed to make a new variable for each drive to individually track percentages
foreach ($Drive in $Drives){
$Percentage = Get-BitLockerVolume | Where-Object -Property "EncryptionPercentage" | Select-Object -ExpandProperty "EncryptionPercentage"
}
#this will continually recheck the percentage until it gets to 100 percent but I need to make it so it will have a while statement for each percentage variable created.
while ($Percentage -LT 100) {
$Percentage = Get-BitLockerVolume | Where-Object -Property "EncryptionPercentage" | Select-Object -ExpandProperty "EncryptionPercentage"
sleep -Seconds 10
}
Get-BitLockerVolume
只有 returns 个有效目标卷,所以我不会太担心光驱。
您不需要创建 2 个变量来存储百分比 - 只需使用 Where-Object
测试 any 音量是否仍低于 100,然后休眠:
# Fetch any drives that aren't already fully encrypted
$unencryptedVolumes = Get-BitLockerVolume | Where-Object VolumeStatus -ne FullyEncrypted
$SecureString = ConvertTo-SecureString "test" -AsPlainText -Force
# Encrypt each volume with the give password.
$unencryptedVolumes |Enable-BitLocker -EncryptionMethod Aes256 -Password $SecureString -PasswordProtector -SkipHardwareTest
# Keep sleeping until all volumes are fully encrypted
while ($waitingFor = Get-BitLockerVolume | Where-Object VolumeStatus -ne FullyEncrypted) {
# Print list of in-progress volumes to the screen, sleep
$waitingFor |Format-Table MountPoint,VolumeStatus,EncryptionPercentage
Start-Sleep -Seconds 10
}
要按所述回答您的问题,您需要使用 set-variable。或 get-variable.
这个命令会store/retrieve个变量,变量可以通过另一个变量命名。这很棘手,为了进行数学运算,您需要将它们组合起来。
例如
Foreach ($number in 1..20) {
Set-Variable -Name name$number -Value "this is number $number"
Get-Variable -Name name$number
}
您也可以使用 Remove-Variable
或 New-Variable
但是 New-Variable
基本上被 Set-Variable
取代,而且,如果变量存在,New-Variable
会报错
为了做数学,它变得更棘手,因为你不能只调用变量并添加 1
所以这是非常无效的,但类似于:
foreach ($number in 1..20) {
Set-Variable -Name name$number -Value "$((get-variable -name name$number) + 1)"
Get-Variable -Name name$number
}
这可以将它递增 1。但是现在您要调用大量的东西来做一个简单的 name1+=1
或 name1++
你也可以做一些花哨的事情,比如
Foreach ($number in 1..20) {
Set-Variable -Name "name$({0:D2} -f $number)" -Value "this is number $number"
Get-Variable -Name "name$({0:D2} -f $number)"
}
你拥有的越多,你就越需要编码和获取(就像上面调用变量是一个长字符串)。正如另一个答案所说,通常有一个更简单的选择。
我正在尝试制作一个脚本来检查 bitlocker 加密百分比并将其设置为一个变量,但对于每个驱动器。
例如。我 运行 我的脚本将在 2 个驱动器上开始 bitlocker 加密。我需要一个 foreach 语句来创建一个变量 foreach 驱动器 (2),然后我将有一个 while 语句表示当驱动器小于 100% 时,重新检查百分比。一旦达到 100%,它将锁定驱动器。
我不知道如何编写代码以便让它为每个检测到并开始加密的驱动器创建一个 while 语句。
对不起,如果这让我感到困惑,我是新手。
这是我到目前为止的代码。 (不要 运行 在你的电脑上,因为它会位锁你的驱动器,哈哈,如果你想 运行 它删除“Enable-Bitlocker”代码)
#Finds all drives connected to the computer, I'm not sure if this will also detect CDs though, but if it does let me know since they can't be bitlocked
$Drives = Get-BitLockerVolume | Where-Object -Property "MountPoint" | Select-Object -ExpandProperty "MountPoint"
#Encrypts the password so it can't be displayed with a read-host command
$SecureString = ConvertTo-SecureString "test" -AsPlainText -Force
#This will run the bitlocker command and encrypt each drive with the secure password.
foreach ($Drive in $Drives){
Enable-BitLocker -MountPoint $Drive -EncryptionMethod Aes256 -Password $SecureString -PasswordProtector -SkipHardwareTest -WhatIf #WhatIf added for safety measures
}
#this is supposed to make a new variable for each drive to individually track percentages
foreach ($Drive in $Drives){
$Percentage = Get-BitLockerVolume | Where-Object -Property "EncryptionPercentage" | Select-Object -ExpandProperty "EncryptionPercentage"
}
#this will continually recheck the percentage until it gets to 100 percent but I need to make it so it will have a while statement for each percentage variable created.
while ($Percentage -LT 100) {
$Percentage = Get-BitLockerVolume | Where-Object -Property "EncryptionPercentage" | Select-Object -ExpandProperty "EncryptionPercentage"
sleep -Seconds 10
}
Get-BitLockerVolume
只有 returns 个有效目标卷,所以我不会太担心光驱。
您不需要创建 2 个变量来存储百分比 - 只需使用 Where-Object
测试 any 音量是否仍低于 100,然后休眠:
# Fetch any drives that aren't already fully encrypted
$unencryptedVolumes = Get-BitLockerVolume | Where-Object VolumeStatus -ne FullyEncrypted
$SecureString = ConvertTo-SecureString "test" -AsPlainText -Force
# Encrypt each volume with the give password.
$unencryptedVolumes |Enable-BitLocker -EncryptionMethod Aes256 -Password $SecureString -PasswordProtector -SkipHardwareTest
# Keep sleeping until all volumes are fully encrypted
while ($waitingFor = Get-BitLockerVolume | Where-Object VolumeStatus -ne FullyEncrypted) {
# Print list of in-progress volumes to the screen, sleep
$waitingFor |Format-Table MountPoint,VolumeStatus,EncryptionPercentage
Start-Sleep -Seconds 10
}
要按所述回答您的问题,您需要使用 set-variable。或 get-variable.
这个命令会store/retrieve个变量,变量可以通过另一个变量命名。这很棘手,为了进行数学运算,您需要将它们组合起来。
例如
Foreach ($number in 1..20) {
Set-Variable -Name name$number -Value "this is number $number"
Get-Variable -Name name$number
}
您也可以使用 Remove-Variable
或 New-Variable
但是 New-Variable
基本上被 Set-Variable
取代,而且,如果变量存在,New-Variable
会报错
为了做数学,它变得更棘手,因为你不能只调用变量并添加 1
所以这是非常无效的,但类似于:
foreach ($number in 1..20) {
Set-Variable -Name name$number -Value "$((get-variable -name name$number) + 1)"
Get-Variable -Name name$number
}
这可以将它递增 1。但是现在您要调用大量的东西来做一个简单的 name1+=1
或 name1++
你也可以做一些花哨的事情,比如
Foreach ($number in 1..20) {
Set-Variable -Name "name$({0:D2} -f $number)" -Value "this is number $number"
Get-Variable -Name "name$({0:D2} -f $number)"
}
你拥有的越多,你就越需要编码和获取(就像上面调用变量是一个长字符串)。正如另一个答案所说,通常有一个更简单的选择。