header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Cant use the data
Last Post 14 Feb 2008 11:35 PM by halr9000. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
saungkhUser is Offline
New Member
New Member
Posts:2
Avatar

--
14 Feb 2008 07:06 PM  

 

 

Hello All,

 

I need some help. I am trying to create a script that will dynamically read all attached USB drives and then allow me to write a common file to each USB drive attached to the pc.

I have figured out how dynamically enumerate through the drives attached to the machine but I have no way to reuse the gathered information in a loop of some sort that will enable me to write the common file to each drive name that I have collected.

My code to do the dynamic enumeration is below - can anyone point me in the right direction?

Get-PSDrive -PSProvider FileSystem | select-object Name | where {$_.Name -ne "C" -AND $_.Name -ne "D"}

 

Thanks,

SNAM

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
14 Feb 2008 07:57 PM  

Try this.


$drives = Get-PSDrive -PSProvider FileSystem | select-object Name | where {$_.Name -ne "C" -AND $_.Name -ne "D"}
$drives | foreach-object {
  set-location $_
  # write files here using set-content or add-content
}
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
saungkhUser is Offline
New Member
New Member
Posts:2
Avatar

--
14 Feb 2008 10:07 PM  

Nope no cigar - it does not seem to allow a parameter to be passed to it I get the following error

Set-Location : Cannot find path 'C:\Windows\System32\@{Name=E}' because it does not exist.
At C:\powershell\usbcopy.ps1:2 char:39
+ $drives | foreach-object {set-location  <<<< $_}

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
14 Feb 2008 11:35 PM  
My mistake, I did not look as closely to your code as I should have. Drop the Select-Object, its unnecessary and has side-effects you may not be aware of.

"Get-psdrive | ? {}" returns a PSdriveinfo object
"get-psdrive | select-object foo | ? {}" returns a custom object with one property named Foo

Only use select-object in cases such as:
- you want to use its -first, -last, and other filtering parameters
- you want to create a custom object which is a subset of another object

Read http://bsonposh.com, he has covered this topic a lot. search his blog for 'custom object'.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer