Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Append Text to Each Item in a PowerShell Array

Posted on 2015-02-13
8
Medium Priority
?
87 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 71

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 1000 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 71

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 71

Accepted Solution

by:
Qlemo earned 1000 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…

688 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