Solved

Powershell

Posted on 2011-09-20
9
681 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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