How do you make a foreach create multiple variables? - powershell

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
}

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
}

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.

Related

Why Powershell outputting this table?

I'm a powershell noob. How come the following code is also outputing the table at the end after the "File to Delete" loop?
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
# use partial hashes for files larger than 100KB:
# see documentation at: https://powershell.one/tricks/filesystem/finding-duplicate-files#finding-duplicate-files-fast
$result = Find-PSOneDuplicateFileFast -Path '\\READYNAS\Pictures\2020\10' #-Debug -Verbose
$stopwatch.Stop()
# output duplicates
$allFilesToDelete = #(foreach($key in $result.Keys)
{
#filters out the LAST item in the array of duplicates, because a file name of xxxx (0) comes before one without the (0)
$filesToDelete = $result[$key][0..($result[$key].count - 2)]
#add each remaining duplicate file to table
foreach($file in $filesToDelete)
{
$file |
Add-Member -MemberType NoteProperty -Name Hash -Value $key -PassThru |
Select-Object Hash, Length, FullName
}
}
)
$allFilesToDelete | Format-Table -GroupBy Hash -Property FullName | Out-String | Write-Host
$allFilesToDelete | Sort-Object -Property FullName -OutVariable allFilesToDelete
$allFilesToDelete | Format-Table -Property FullName | Out-String | Write-Host
$confirmation = Read-Host "Are you Sure You Want To Delete $($allFilesToDelete.count) files? (y/n)"
if ($confirmation -eq 'y') {
$i = 0
foreach($fileToDelete in $allFilesToDelete)
{
$i++
Write-Host "$i File to Delete: $($fileToDelete.FullName)"
#Remove-Item $file.FullName -Force -Verbose 4>&1 | % { $x = $_; Write-Host "Deleted file ($i) $x" }
}
} else {
Write-Host "User chose NOT to delete files!"
}
$allFilesToDelete | Sort-Object -Property FullName -OutVariable allFilesToDelete produces output (the input objects in the requested sort order), and since you're not capturing or redirecting it, it prints to the host (display, terminal) by default.
It seems your intent is to sort the objects stored in $allFilesToDelete, which your command does, but it also produces output (the common -OutVariable parameter does not affect a cmdlet's output behavior, it simply also stores the output objects in the given variable); you could simply assign the output back to the original variable, which wouldn't produce any output:
$allFilesToDelete = $allFilesToDelete | Sort-Object -Property FullName
In cases where actively suppressing (discarding) output is needed, $null = ... is the simplest solution:
See this answer for details and alternatives.
Also see this blog post, which you found yourself.
Because the output resulted in implicitly Format-Table-formatted display representations (for custom objects that have no predefined formatting data), the subsequent Read-Host and Write-Host statements - surprisingly - printed first.
The reason is that this implicit use of Format-Table results in asynchronous behavior: output objects are collected for 300 msecs. in an effort to determine suitable column widths, and during that period output to other output streams may print.
The - suboptimal - workaround is to force pipeline output to print synchronously to the host (display), using Out-Host.
See this answer for details.

Powershell with progress bar is not writing results to a file, file remains blank

I'm trying to scan the network, and write the PC name of each corresponding ip to a text file. It was working, until I put the progress bar code in place. Now it will create the blank file, but never writes anything to it.
I was using Add-Content instead of Out-File, but that doesn't create the text file at all.
#Declare IP range
$range = 1..254
$address = “192.168.0.$_”
#status
Write-Output "Scanning active PCs"
#Scan ip range and get pc names
$range | ForEach-Object {Write-Progress “Scanning Network” $address -PercentComplete (($_/$range.Count)*100) | Start-Sleep -Milliseconds 100 | Get-WmiObject Win32_PingStatus -Filter "Address='192.168.0.$_' and Timeout=200 and ResolveAddressNames='true' and StatusCode=0 and ProtocolAddressResolved like '%.domain.com'" | select -ExpandProperty ProtocolAddressResolved} | Out-File C:\PowershellScripts\ComputerList.txt
A resonable formatting would have helped you (and others) to understand your code much easier:
#Declare IP range
$range = 1..254
$address = "192.168.0."
#status
Write-Output "Scanning active PCs"
#Scan ip range and get pc names
$range |
ForEach-Object {
Write-Progress 'Scanning Network' $address$_ -PercentComplete (($_ / $range.Count) * 100)
Start-Sleep -Milliseconds 100
Get-WmiObject Win32_PingStatus -Filter "Address='192.168.0.$_' and Timeout=200 and ResolveAddressNames='true' and StatusCode=0 and ProtocolAddressResolved like '%.domain.com'" |
Select-Object -ExpandProperty ProtocolAddressResolved
} |
Out-File C:\PowershellScripts\ComputerList.txt
Write-Output does not generate any ouptut. So it does not make sense to pipe it to any other cmdlet.
If you really want to have 2 different and unrelated commands on one line you should separate them with a semikolon like Thomas already mentioned in the comments above.
That's the same for Start-Sleep by the way. I'd recommend to always read the complete help for the cmdlets you're about to use to learn how to use them.
To make your code easier to read you should use line breaks and indentation. Here is some more to read about: The PowerShell Best Practices and Style Guide

Unable to show export-csv in PoweSshell

I have been researching the web to see what am I missing and can't find out, I run the command it goes thru the list of computers but the export doc is always empty.
Here is the code
foreach ($computer in Get-Content "\\NETWORK PATH\user-computers.txt") {
Write-host $computer
$colDrives = Get-WmiObject Win32_MappedLogicalDisk -ComputerName $computer
$Report = #()
# Set our filename based on the execution time
$filenamestring = "$computer-$(get-date -UFormat "%y-%b-%a-%H%M").csv"
foreach ($objDrive in $colDrives) {
# For each mapped drive – build a hash containing information
$hash = #{
ComputerName = $computer
MappedLocation = $objDrive.ProviderName
DriveLetter = $objDrive.DeviceId
}
# Add the hash to a new object
$objDriveInfo = new-object PSObject -Property $hash
# Store our new object within the report array
$Report += $objDriveInfo
}}
# Export our report array to CSV and store as our dynamic file name
$Report | Export-Csv -LiteralPath "\\NETWORK PATH\Drive-Maps.csv" -NoTypeInformation
I want to know what each computer currently got mapped network drives, thanks for all your help and guidance.
I'm not sure why you're not getting output. I've rewritten your script for a few reasons I'd like to point out. First, your variable naming is not very clear. I'm guessing you come from a VBScripting background. Next, you're creating an array and then adding to it - this is simply not needed. You can capture the output of any loop/scriptblock/etc directly by assigning like tihs.
$Report = foreach($thing in $manythings){Do lots of stuff and everything in stdout will be captured}
If you write your script in a way that takes advantage of the pipeline, you can do even more. Next, creating the object with New-Object is slow compared to using the [PSCustomObject] type accelerator introduced in V3. Finally, it seems you create a custom csv for each computer but in the end you just export everything to one file. I'm going to assume you are wanting to collect all this info and put in one CSV.
My recommendation for you to help troubleshoot, run this against your machines and confirm the output on the screen. Whatever you see on the screen should be captured in the report variable. (Except write-host, it's special and just goes to the console)
$computerList = "\\NETWORK PATH\user-computers.txt"
$reportFile = "\\NETWORK PATH\Drive-Maps.csv"
Get-Content $computerList | ForEach-Object {
Write-host $_
$mappedDrives = Get-WmiObject Win32_MappedLogicalDisk -ComputerName $_
foreach ($drive in $mappedDrives)
{
# For each mapped drive – build a hash containing information
[PSCustomObject]#{
ComputerName = $_
MappedLocation = $drive.ProviderName
DriveLetter = $drive.DeviceId
}
}
} -OutVariable Report
Once you know you have all the correct info, run this to export it.
$Report | Export-Csv -LiteralPath $reportFile -NoTypeInformation

Power shell For Loop not Looping

So the output works fine but I'm having an issue with it only outputing the last line it runs. Is there anyway to check for loops to test in the future?
but i have a list of ip address and im trying to check if the firewall in windows is enabled or disabled.
They are on one LARGE (300+ workgroup). Any help in getting this to loop properly would be appreciated. Security and other things are not a concern cause i have other scripts that run fine. And i dont get any errors. just the single output.
ive already tried moving the array and that didn't help. im thinking it could be the PSCustomObject part as i'm just starting to learn these. Or could it be my input and output formats are different and that's causing issues??
clear
$ComputerList = get-content C:\Users\Administrator\Desktop\DavidsScripts\TurnOffFirewall\input.txt
$Status = #(
foreach ($Computer in $ComputerList) {
netsh -r $Computer advfirewall show currentprofile state})[3] -replace 'State' -replace '\s'
$Object = [PSCustomObject]#{
Computer = $Computer
Firewall = $Status
}
Write-Output $Object
$Object | Export-Csv -Path "C:\FirewallStatus.csv" -Append -NoTypeInformation
Your previous code was not escaping the loop and was only adding the last computer in the loop to the object.
The best way I have found, is to make a temp object and add it to an array list then export that. Much nicer.
$ComputerList = get-content C:\Users\Administrator\Desktop\DavidsScripts\TurnOffFirewall\input.txt
$collectionVariable = New-Object System.Collections.ArrayList
ForEach ($Computer in $ComputerList) {
# Create temp object
$temp = New-Object System.Object
# Add members to temp object
$temp | Add-Member -MemberType NoteProperty -Name "Computer" -Value $Computer
$temp | Add-Member -MemberType NoteProperty -Name "Firewall" -Value $((netsh -r $Computer advfirewall show currentprofile state)[3] -replace 'State' -replace '\s')
# Add the temp object to ArrayList
$collectionVariable.Add($temp)
}
Write-Output $collectionVariable
$collectionVariable | Export-Csv -Path "C:\FirewallStatus.csv" -Append -NoTypeInformation
Here's a streamlined, functional version of your code, using a single pipeline:
Get-Content C:\Users\Administrator\Desktop\DavidsScripts\TurnOffFirewall\input.txt |
ForEach-Object {
[pscustomobject] #{
Computer = $_
Firewall = (-split ((netsh -r $_ advfirewall show currentprofile state) -match '^State'))[-1]
}
} | Export-Csv -Path C:\FirewallStatus.csv -NoTypeInformation
Note:
No intermediate variables are needed; each computer name read from the input file is processed one by one, and each custom object constructed based on it is sent to the output CSV file.
The command for extracting the firewall status from netsh's output was made more robust in order to extract the state information based on the line content (regex ^State, i.e., a line starting with State) rather than a line index ([3]); the unary form of -split splits the line of interest into tokens by whitespace, and index [-1] extracts the last token, which is the state value.
As for what you tried:
Your foreach loop ended before $Object was constructed, so you ended up constructing just 1 object to send to the output file with Export-Csv.
If you had formatted your code properly, that fact would have been more obvious; try using Visual Studio Code with the PowerShell extension, which offers automatic formatting via the >Format Document (Shift+Alt+F) command.

Out-file format

I am writing a script that after each iteration through a loop (array of selected services) it will gather the 4 values for each service that are: server name, service name, service state, and service start name
So for each iteration, I would like to output the 4 mentioned values to an external file (txt, svc, or html) such that each value will be arranged in its own column. Currently I use tab `t to arrange the values in each column but it doesn't work quite well because some service name is a lot longer or a lot shorter so it screws up the column alignment. What other approach do you suggest so all columns are aligned properly
Below is a snippet of my script on how I currently format the output to a txt file
ForEach($service in services)
$startname = $service.startname
$state = $service.state
$servicename = $service.name
write-output "$server `t $servicename `t $state `t $startname is current" | out-file -append $ScriptDirectory
If you just want to dump the results to text in a nicely-formatted way (i.e. you don't have requirements for making this CSV, or tab-delimited, or anything else besides "easy for a person to read"), then just use Format-Table -AutoSize.
AutoSize does exactly what you want - it inspects the length of all properties you are outputting, then dynamically adjusts the column width so that as much as possible is shown.
You don't explain where $server comes from, I will assume that is defined somewhere else...
$services `
| Format-Table -AutoSize #{N='Server';E={$server}},StartName,State,Name `
| Out-String `
| Out-File results.txt
Instead of using several variables, use a Powershell object to store your output. Something like this:
ForEach($service in $services) {
New-Object PSObject -Property #{
StartName = $service.startname
State = $service.state
ServiceName = $service.name
}
} | Out-File $ScriptDirectory
You may need to add a Select-Object in the chain to ensure the columns are in the correct order that you want for your final output.
If you want to keep the variables, You could try the following String formatting to space out the variable in the string evenly. In the example below the spacing is 20 characters between each value:
ForEach($service in services){
$startname = $service.startname
$state = $service.state
$servicename = $service.name
"{0,-20} | {1,-20} | {2,-20} | {3,-20}" -f $server,$servicename,$state,$startname `
| Out-File -append $ScriptDirectory
}
It's a little unclear what you're looking for as some of the properties of the object Get-Service returns don't exist as written and the code seems incomplete. Taking a guess at your intent though:
$servers = #("server1","server2");
$services = get-service -computername $servers;
$svcCollection = #();
ForEach($service in $services) {
$svccollection+=New-Object PSObject -Property #{
Servername = $service.MachineName;
StartName = $service.servicename;
State = $service.Status;
ServiceName = $service.DisplayName;
}
}
# Various output formats
$svccollection|ConvertTo-Html|Out-File -path Services.html; # Create a full HTML file
$svcCollection|Export-Csv -NoTypeInformation -Path Services.csv; # Create a "traditional" CSV file
$svcCollection|Export-Csv -Delimiter "`t" -Path Services-tab.csv; # Create a tab-delimited CSV file
$svcCollection|ConvertTo-Xml|Out-File -path Services.xml; # Create an XML file
$svcCollection|ConvertTo-Json|Out-File -path Services.js; # Create a JSON object (v3 only)