Link to home
Start Free TrialLog in
Avatar of Alex
AlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Yet another, arduous powershell question

$computers = Get-content 'C:\Powershell Projects\lastconnect\New Text Document.txt'
Foreach ($computer in $computers)
 {Copy-Item -Path '\\$Computer\Program Files (x86)\Quest Software\Toad for Oracle 10.5\ClientFiles\QSAuth11.key' -Destination 'c:\programs\powershell projects\$computer.txt'}

Open in new window


Afternoon,

So the script above, I know I can't put the variable in like that, but I can't find or figure out how to get it to work the way I need it to.

Could you help at all?

Cheers
Alex
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

Ahhhhh you're back!!!!

So it was the $ at the start and the double quotes..

I even put double quotes in to start but then thought I'd go with the single quotes.... Woe is me :(

Right,

I'm up to this now

$Computers = Get-content 'C:\Powershell Projects\audit\computers.txt'
ForEach ($Computer in $Computers){
    If (test-connection -computername $Computer -quiet) 
 {
	Copy-Item -Path "\\$($Computer)\c$\Program Files (x86)\Quest Software\Toad for Oracle 10.5\ClientFiles\QSAuth11.key" -Destination "C:\powershell projects\$($Computer).txt"
}

Else { 
        "$($computer) is not online" | Out-File -Append "C:\Powershell Projects\\computersnotonline.txt"
      }
}

Open in new window

Avatar of Alex

ASKER

Perfect
Note: It would have been sufficient to use "\\$Computer\c$\...", but that only works if parsing allows for clear detection where the variable name ends. "\\${Computer}\c$\..." is more safe, but then it is not far from generally using "$($Computer)\c$\...", which technically uses a subexpression ( $(...) is a subexpression, allowing for expressions).