Solved

Append Text to Each Item in a PowerShell Array

Posted on 2015-02-13
8
29 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
  • 3
  • 3
8 Comments
 
LVL 68

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 16

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 68

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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…
This video discusses moving either the default database or any database to a new volume.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now