Solved

Powershell

Posted on 2011-09-20
9
675 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

16 Experts available now in Live!

Get 1:1 Help Now