Solved

Powershell

Posted on 2011-09-20
9
678 Views
Last Modified: 2012-11-30
Dear Experts;

I have a powershell that reads a list of computers and copies a file to their desktop. I need to add an if statment to this code that would see if the folder in C:\Program Files\Microsoft Office\Office12 (if computer has office 2007) exists in the target computer and if it does then copies the files in to the computer but if the folder does not exist then does not copy the file and moves on to the next computer in the text file. (the list of computers are in a text file)
FYI: I am a beginner in powershell and have no time to search for this so please HELP :)

$Computers = get-qadcomputer
foreach ($Computer in $Computers){
$CompName = $Computer.name
"Attempting to copy files to $CompName..."
try {$Copy = Copy-Item C:\\TEST.application "\\ComputerName\c$\Documents and Settings\All Users\Desktop"
if ($?){"$CompName Success" >> UpdateLog.txt}else{"$CompName Failed" >> UpdateLog.txt}
} #end try
Catch {}
Finally {}
} #end foreach
0
Comment
Question by:pauledwardian
  • 5
  • 3
9 Comments
 
LVL 8

Expert Comment

by:Brent Challis
ID: 36571589
The easiest way to test if the folder exists is to use the Test-Path cmdlet.  
e.g. test-path -path "C:\Documents and Settings\NicoleH"

If the path exists the cmdlet return $True, otherwise $False so you can use it as the test condition in the if statement.
0
 

Author Comment

by:pauledwardian
ID: 36574054
Can you provide the code please? Liek I said its urgent and I dont have time to figure it out myself.
0
 
LVL 2

Expert Comment

by:feaglin
ID: 36575394
This should do it.  I changed it a bit because I haven't used try-catch a lot.  Test-path will return true if the directory exists.


$Computers = get-qadcomputer

foreach ($Computer in $Computers){
      $CompName = $Computer.name
      "Attempting to copy files to $CompName..."
      if (test-path "C:\Program Files\Microsoft Office\Office12") {
            Copy-Item C:\\TEST.application "\\ComputerName\c$\Documents and Settings\All Users\Desktop"
            "$CompName Success" >> UpdateLog.txt  }
      else {
            "$CompName Failed" >> UpdateLog.txt }
} #end foreach

0
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.

 

Author Comment

by:pauledwardian
ID: 36577666
feaglin,

Would this code work if I have the name of computers in the textfile like computers.txt and does it go through each computer in the text file one by one?

Paul
0
 
LVL 2

Expert Comment

by:feaglin
ID: 36577824
Yes.  Just replace

     $Computers = get-qadcomputer

with

     $Computers = get-content computernames.txt

hth
0
 

Author Comment

by:pauledwardian
ID: 36578521
So like:
Please verify its pretty urgent

 $Computers = get-content computernames.txt

foreach ($Computer in $Computers){
      $CompName = $Computer.name
      "Attempting to copy files to $CompName..."
      if (test-path "C:\Program Files\Microsoft Office\Office12") {
            Copy-Item C:\\TEST.application "\\ComputerName\c$\Documents and Settings\All Users\Desktop"
            "$CompName Success" >> UpdateLog.txt  }
      else {
            "$CompName Failed" >> UpdateLog.txt }
} #end foreach
0
 

Author Comment

by:pauledwardian
ID: 36578554
What I'm not sure is that if it doesn't go through each computer in the text file!
Can you verify this please?
0
 
LVL 2

Accepted Solution

by:
feaglin earned 500 total points
ID: 36582048
I'm on a different computer today that I can test this better on.  It worked on my computer, so hopefully that matches your environment.

1- You will know the computers that you'll attempt to update because of line 5 and your logfile.

2- Line 4 I removed ".Name" from "$Computer".  Getting the names from the text file means you're passing the names in as text instead of an AD object you have to specify the name on.  If you switch back to getting the computernames from AD then you'll have to put the ".Name" back, like you have it in your original script.

3- Line 6 I changed to test the possible paths for 32 bit and 64 bit versions of Windows.  I also fixed the path to check.  You were testing the local computer and copying to a remote computer.  I'm assuming you want to copy an application from your computer to computers on the network that have MS Office installed.
    Note: The extra $() around $Compname is to get the variable properly recognized when it's in double quotes.

4- Line 7 I fixed the variable name (typo) and fixed it to work in a string.


$Computers = get-content computernames.txt

foreach ($Computer in $Computers){
      $CompName = $Computer
      "Attempting to copy files to $CompName..."
      if ( (test-path "\\$($CompName)\c$\Program Files\Microsoft Office\Office12") -or (test-path "\\$($CompName)\c$\Program Files (x86)\Microsoft Office\Office12") ) {
            Copy-Item C:\\TEST.application "\\$($CompName)\c$\Documents and Settings\All Users\Desktop"
            "$CompName Success" >> UpdateLog.txt  }
       else {
            "$CompName Failed" >> UpdateLog.txt }
} #end foreach

Open in new window

0
 

Author Closing Comment

by:pauledwardian
ID: 36714574
Thanks a lot.
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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A procedure for exporting installed hotfix details of remote computers using powershell
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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