Knowlesy
Knowlesy

Reputation: 1

Powershell: how to user input array, cycling through it and then closing out at the end

Im trying to create a powershell script for FSRM that will create reports for each drive. Im trying to ask the end user how many drives they wish to monitor then a follow up of the drive letter it will then create a report per drive letter however Im stuck at creating the array size and getting the information into the array followed by stopping the report creation when they are all done

#Asks user for how many disks they want to have reports created for them
$Localdisks = $Null
#Sets Array size for local disk
$Localdiskarraynumber = @($localdiskarray)
#Counts how many items are in the array to how many have been applied
$localdiskarraysize = $Localdiskarray.count
#Set Size of array
$Localdiskarray = Read-Host -Prompt 'Input the amount of local disks you want to run reports on'
#Set report Name (report per disk)
$Reportname = "$env:computername-$localdisk-Report"
#Ask users for storage report email address
$EmailReport = Read-Host -Prompt 'Input your Email address for Reports:'
#Sets time for report
$d = get-date "08:30am"
#Sets day for report / weekly
$task = New-FsrmScheduledTask -Time $d -Weekly @("Monday")
Do
{
$localdisks = Read-Host -Prompt 'Disk Letter'
#report created per letter entered
New-FsrmStorageReport -Name $Reportname -Namespace "$Localdisks:\" -Schedule $task -ReportType @("LargeFiles", "DuplicateFiles", "FilesByFileGroup", "FilesByOwner", "LeastRecentlyAccessed", "MostRecentlyAccessed") -LargeFileMinimum 500MB -FileGroupIncluded @("Virus FileTypes", "3D FileTypes", "Archive File Types", "Audio FileTypes", "Database FileTypes", "Disk FileTypes", "Executables FileTypes", "Font FileTypes", "Image FileTypes", "MS Office FileTypes", "Office FileTypes", "Suspicious FileTypes", "Video FileTypes") -LeastAccessedMinimum 90 -MostAccessedMaximum 91 -MailTo $EmailReport
}
#Stops Do when full cycle through of the array is complete
until ($localdiskarraysize = $Localdisks)

Upvotes: 0

Views: 972

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 29033

Im trying to ask the end user how many drives they wish to monitor then a follow up of the drive letter it will then create a report per drive letter however Im stuck at creating the array size and getting the information into the array followed by stopping the report creation when they are all done

This is very un-PowerShelly design. Instead of asking how many, ask the user for drives until they stop entering more. There's no real concept of creating an array of a particular size (I guess you could force a way of doing it, but it's not common or really relevant here).

Something like

# Keep prompting for another drive until they just press enter
$drives = do 
{ 
    $drive = Read-Host "Enter another drive, or blank to finish"
    $drive
} while ($drive -ne '')

# Loop over the drives and generate a report for each one
foreach ($drive in $drives) 
{
    # process drive here
}

Then you don't need to count through them to use them either.


NB. where you try to count through them here:

until ($localdiskarraysize = $Localdisks)

The = in PowerShell is only for assigning values ($a = 1), to test if two things are the same you need to use -eq.

Upvotes: 1

Related Questions