Solved

Powershell

Posted on 2011-09-20
9
676 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:bchallis
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
 

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

18 Experts available now in Live!

Get 1:1 Help Now