Solved

Powershell script explanation

Posted on 2016-10-04
10
59 Views
Last Modified: 2016-10-04
I was wondering if someone could help explain what the below powershell script is doing.  It was inherited and whatever its function isn't working correctly, i was hoping to understand a little better to help troubleshoot

$srcDir = "C:\Opex"
$dstDir = "\\tc00afsv02\c$\Shares\zvol2$\OpexTest\Unprocessed"
$bakDir = "C:\OpexBackup"

while ($true){

    $folders = Get-ChildItem -Path $srcDir

    foreach ($folder in $folders){

        $folderPath = $folder.FullName
        $folderName = $folder

        $oxiExists = Test-Path -Path "$folderPath\*.oxi"

        if ($oxiExists){      
            Write-Output "OXI Found in $folderPath"
            $folderExists = Test-Path -Path $dstDir\$folderName
            if (-not $folderExists){

                New-Item -Path $dstDir\$folderName -ItemType Directory -Verbose

                Copy-Item -Path $folderPath -Destination $bakDir -Recurse -Verbose
				Sleep 1
				Copy-Item -Path "$folderPath\*.tif" -Destination $dstDir\$folderName -Recurse -Verbose
				Sleep 1
				Copy-Item -Path "$folderPath\*.oxi" -Destination $dstDir\$folderName -Recurse -Verbose
                Sleep 1
                Remove-Item -Path $folderPath -Recurse -Force -Verbose
            }
        }

        else {
            Write-Output "OXI Not Found"
            Sleep 1
        }

    }
    Sleep 1
}  

Open in new window

0
Comment
Question by:leadtheway
  • 5
  • 4
10 Comments
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41828059
While ($true) means that the code continuously loops (probably intended to be run as a service).

First, it gets all child items in $srcDir and sets that as a collection of items called $folders.

Then foreach ($folder in $folder) means, for each item in your collection $folders it does the code.

Now it sets the folder path as $folderPath and the name as $folderName.

Test-Path returns true if it finds anything, so it looks for a .oxi file in each folder, so it's checking to see if one exists.

If one does exist, it checks to see if a folder of the same name exists in the destination.  If not, it creates one and then copies everything from the source folder to the $bakDir.  After that it copies all .tif and all .oxi to the new destination folder.

After the copy, it then deletes the source folder.

If it doesn't find anything, on the Test-Path, then it doesn't take any actions.

Since it's all enclosed in a While {} loop, it waits 1 second (Sleep 1) then starts the process again.
0
 
LVL 1

Author Comment

by:leadtheway
ID: 41828173
where would i look to see if it's running as a service?
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41828182
services.msc on any of your main servers would be a good place to check.  If it's not running as a service then it may be firing off as a scheduled task.  If you recently rolled passwords and it's not running, could be the task or service needs the updated password.
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 1

Author Comment

by:leadtheway
ID: 41828193
yeah i have another script that is scheduled that cleansout the backup directory this script copies to, but i don't see one for this script, nor do i see it in services...not sure how it runs then, but it's running because its moving to bakdir, just not copying to the dstdir
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41828207
Is this still a valid location?
$dstDir = "\\tc00afsv02\c$\Shares\zvol2$\OpexTest\Unprocessed"
0
 
LVL 1

Author Comment

by:leadtheway
ID: 41828230
yes
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41828252
You may want to then open the script in powershell ISE and run it manually.  If there are any errors, you'll get red text output and it might shed some light as to where your problem is coming from.
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 41828589
It won't be running as a service but a scheduled task
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41828616
It's fairly popular for people to load powershell tasks as a service, it's actually very easy to create them using PowerGUI.  It's typically preferred to run like that because you can set up your monitoring to check the service status and ensure it's not down.
0
 
LVL 1

Author Comment

by:leadtheway
ID: 41828640
Just a follow up, after doing some digging through event log, it was actually launched and left running constantly checking for that oxi file, it was launched under the profile of the guy I inherited from, thus when his account was disabled, that profile no longer had access to that path.. I think we have it sorted, much thanks
0

Featured Post

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.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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