[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 119
  • Last Modified:

Append Text to Each Item in a PowerShell Array

Hello!

I'm working on a PowerShell script to add a File Server Resource Monitor file screen to each drive in the server. We deal with lots of servers that have any number of drives. What I want to accomplish is holding each drive letter as a variable and then later, spitting those out to apply the file screens.

Here is what I have so far:

$DiskArray= Get-Volume | Select DriveLetter

Open in new window

I then need to add :\ to the end of each drive letter.

From working with FSRM, I see that a new file screen would need to be created for each drive, since you can only add one path. Here is the line I use to manually create a file screen.  

New-FsrmFileScreen -path "E:\" -template "Test Template"

Open in new window

I would then like to repeat this line for every drive, the path being C:\, D:\, E:\, F:\ and so on.

Any help would be greatly appreciated.
0
iVenture_Solutions
Asked:
iVenture_Solutions
  • 3
  • 3
2 Solutions
 
QlemoDeveloperCommented:
Get-Volume | Select DriveLetter | % {
  New-FsrmFileScreen -path $_.DriveLetter+":\" -template "Test Template"
}

Open in new window

or
$DiskArray | % {
  New-FsrmFileScreen -path $_.DriveLetter+":\" -template "Test Template"
}

Open in new window

0
 
iVenture_SolutionsAuthor Commented:
Here is the output I get with the first option.
% : A positional parameter cannot be found that accepts argument '+:\'.
At C:\Users\Administrator\Downloads\0.ps1:1 char:35
+ Get-Volume | Select DriveLetter | % {
+                                   ~~~
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.ForEachObject 
   Command

Open in new window


and with the second:

New-FsrmFileScreen : A positional parameter cannot be found that accepts argument '+:\'.
At C:\Users\Administrator\Downloads\1.ps1:3 char:3
+   New-FsrmFileScreen -path $_.DriveLetter+":\" -template "Test Template"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-FsrmFileScreen], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,New-FsrmFileScreen
 
New-FsrmFileScreen : A positional parameter cannot be found that accepts argument '+:\'.
At C:\Users\Administrator\Downloads\1.ps1:3 char:3
+   New-FsrmFileScreen -path $_.DriveLetter+":\" -template "Test Template"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-FsrmFileScreen], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,New-FsrmFileScreen
 
New-FsrmFileScreen : A positional parameter cannot be found that accepts argument '+:\'.
At C:\Users\Administrator\Downloads\1.ps1:3 char:3
+   New-FsrmFileScreen -path $_.DriveLetter+":\" -template "Test Template"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-FsrmFileScreen], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,New-FsrmFileScreen
 
New-FsrmFileScreen : A positional parameter cannot be found that accepts argument '+:\'.
At C:\Users\Administrator\Downloads\1.ps1:3 char:3
+   New-FsrmFileScreen -path $_.DriveLetter+":\" -template "Test Template"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-FsrmFileScreen], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,New-FsrmFileScreen
 
New-FsrmFileScreen : A positional parameter cannot be found that accepts argument '+:\'.
At C:\Users\Administrator\Downloads\1.ps1:3 char:3
+   New-FsrmFileScreen -path $_.DriveLetter+":\" -template "Test Template"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-FsrmFileScreen], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,New-FsrmFileScreen
 

Open in new window

0
 
LearnctxEngineerCommented:
Must be how they have written the cmdlet because that should work. Maybe try encapsulate the command into parenthesis which will tell PowerShell to execute this before the rest of the command.

Get-Volume | Select DriveLetter | % {
  New-FsrmFileScreen -path ($_.DriveLetter+":\") -template "Test Template"
}
## Or
Get-Volume | Select DriveLetter | % {
  New-FsrmFileScreen -path "$($_.DriveLetter):\" -template "Test Template"
}

Open in new window


$DiskArray | % {
  New-FsrmFileScreen -path ($_.DriveLetter+":\") -template "Test Template"
}
## Or
$DiskArray | % {
  New-FsrmFileScreen -path "$($_.DriveLetter):\" -template "Test Template"
}

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
QlemoDeveloperCommented:
Those changes should work. The cmdlet seems to expect and force a string, so gets confused by a string expression as I used it.
0
 
iVenture_SolutionsAuthor Commented:
Sorry for my delayed response.

Here is what I came up with to make it work.

foreach ($item In $DiskArray) {
  $temp = $item.DriveLetter
  $temp += ":\" 
  write-host "Running Script for Drive: " + $temp
  New-FsrmFileScreen -path $temp -Active:$false -template "Test Template" -Notification $Notification

Open in new window


The only issue I've been unable to get past now is getting rid of the blank space for the System Reserved Drive Letter. I have tried a few methods to get rid of the blank or null space, but have had no luck. I am still pulling the variable as:

$DiskArray= Get-Volume | Select DriveLetter

Open in new window

0
 
QlemoDeveloperCommented:
get-volume | ? { $_.DriveLetter } | % {
  $temp = $_.DriveLetter + ':\'
  write-host "Running Script for Drive: " + $temp
  New-FsrmFileScreen -path $temp -Active:$false -template "Test Template" -Notification $Notification
}

Open in new window

0
 
iVenture_SolutionsAuthor Commented:
That is perfect. Thank you very much!
0

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now