您如何使foreach创建多个变量?

发布于 2025-01-23 02:26:54 字数 1710 浏览 0 评论 0原文

我正在尝试制作一个脚本,该脚本将检查Bitlocker加密百分比并将其设置为变量,但对于每个驱动器。

例如。我运行我的脚本,该脚本将在2个驱动器上开始Bitlocker加密。我需要一个foreach语句,它将创建一个变量for for for for for foreach drive(2),然后我将有一定的声明说,虽然驱动器不到100%,但重新检查百分比。它不止一次达到100%,它将锁定驱动器。

我不知道如何编写代码,以便我可以为检测到并开始加密的每个驱动器创建一个wirs语句。

对不起,如果这让我很新来。

到目前为止,我的代码。 (请勿在计算机上运行,​​因为如果要运行的话,它会将您的驱动器锁定,如果要删除“ 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
}

I'm trying to make a script that will check a bitlocker encryption percentage and set it as a variable but for each drive.

For example. I run my script that will begin bitlocker encryption on 2 drives. I need a foreach statement that will create a variable foreach drive (2), then I will have an while statement that says while a drive is less than 100 percent, recheck the percentage. Than once it hits 100 percent it will lock the drive.

I don't know how to write code so that I can have it create a while statement for each drive that is detected and has started encrypting.

Sorry if this is confusing I'm new.

Heres my code so far. (DO NOT RUN ON YOUR COMPUTER BECAUSE IT WILL BITLOCK YOUR DRIVES lol, IF YOU WANT TO RUN IT DELETE THE "Enable-Bitlocker" CODE)

#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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2025-01-30 02:26:54

Get-bitlockervolume仅返回有效的目标量,因此我不会太担心光学驱动器。

您无需创建2个变量即可存储这些百分比 - 只需使用where -object即可测试任何卷是否仍然低于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
}

Get-BitLockerVolume only returns valid target volumes, so I wouldn't be too worried about optical drives.

You don't need to create 2 variables to store the percentages - simply use Where-Object to test whether any volume is still under 100, then sleep:

# 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
}
瑾兮 2025-01-30 02:26:54

要回答您的问题,您想使用设置变量。或可转变。

此命令将存储/检索变量,并且可以通过另一个变量命名变量。这很棘手,为了进行数学,您需要将它们结合起来。

例如,

Foreach ($number in 1..20) {
    Set-Variable -Name name$number -Value "this is number $number"
    Get-Variable -Name name$number
}

您还可以使用remove-variablenew-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+= 1name1 ++

您也可以做一些花哨的事情,例如

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)"
}

您拥有的越多,您必须编码越多(例如,将变量称为上面的变量是一个长细绳)。正如另一个答案所说,通常有一个简单的选择。

To answer your question as stated, you want to use set-variable. or get-variable.

This command will store/retrieve variables, and the variables can be named via another variable. This get's tricky, and in order to do math you need to combine them.

for example

Foreach ($number in 1..20) {
    Set-Variable -Name name$number -Value "this is number $number"
    Get-Variable -Name name$number
}

you can also use Remove-Variable or New-Variable
but New-Variable is basically superseded by Set-Variable as well as, if a variable exists, New-Variable will spit out an error

in order to do math it get's trickier as you can't just call the variable and add 1

so this is very not effective but something like:

foreach ($number in 1..20) {
    Set-Variable -Name name$number -Value "$((get-variable -name name$number) + 1)"
    Get-Variable -Name name$number
}

this could increment it by 1. but now you're calling a ton of stuff to do a simple name1+=1 or name1++

you can also do some fancy stuff like

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)"
}

the more you have, the more you have to code and get (like calling the variable as above is a long string). As the other answer says, usually there's a much simpler option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文