您如何使foreach创建多个变量?
我正在尝试制作一个脚本,该脚本将检查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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Get-bitlockervolume
仅返回有效的目标量,因此我不会太担心光学驱动器。您无需创建2个变量即可存储这些百分比 - 只需使用
where -object
即可测试任何卷是否仍然低于100,然后睡觉: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:要回答您的问题,您想使用设置变量。或可转变。
此命令将存储/检索变量,并且可以通过另一个变量命名变量。这很棘手,为了进行数学,您需要将它们结合起来。
例如,
您还可以使用
remove-variable
或new-variable
但是
new-variable
基本上由set-variable
以及,如果存在变量,new-variable
将在为了进行数学,它会变得更棘手,因为您不能只调用变量并添加1,
因此这是非常有效的,但类似:
这可以将其增加1个。但是现在您呼吁大量的事情来做一个简单的事情
name1+= 1
或name1 ++
您也可以做一些花哨的事情,例如
您拥有的越多,您必须编码越多(例如,将变量称为上面的变量是一个长细绳)。正如另一个答案所说,通常有一个简单的选择。
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
you can also use
Remove-Variable
orNew-Variable
but
New-Variable
is basically superseded bySet-Variable
as well as, if a variable exists,New-Variable
will spit out an errorin 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:
this could increment it by 1. but now you're calling a ton of stuff to do a simple
name1+=1
orname1++
you can also do some fancy stuff like
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.