Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell

Posted on 2011-09-20
9
Medium Priority
?
684 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
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

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

604 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