Solved

Append Text to Each Item in a PowerShell Array

Posted on 2015-02-13
8
35 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 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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