Solved

Append Text to Each Item in a PowerShell Array

Posted on 2015-02-13
8
43 Views
Last Modified: 2016-06-24
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
Comment
Question by:iVenture_Solutions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 40608404
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
 

Author Comment

by:iVenture_Solutions
ID: 40608739
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
 
LVL 17

Assisted Solution

by:Learnctx
Learnctx earned 250 total points
ID: 40608838
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 69

Expert Comment

by:Qlemo
ID: 40608951
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
 

Author Comment

by:iVenture_Solutions
ID: 40653652
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
 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 40653672
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
 

Author Comment

by:iVenture_Solutions
ID: 40654708
That is perfect. Thank you very much!
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question