Solved

Powershell script explanation

Posted on 2016-10-04
10
68 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 80

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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