Solved

Powershell script explanation

Posted on 2016-10-04
10
42 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
Comment Utility
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
Comment Utility
where would i look to see if it's running as a service?
0
 
LVL 12

Expert Comment

by:Dustin Saunders
Comment Utility
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
 
LVL 1

Author Comment

by:leadtheway
Comment Utility
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
Comment Utility
Is this still a valid location?
$dstDir = "\\tc00afsv02\c$\Shares\zvol2$\OpexTest\Unprocessed"
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:leadtheway
Comment Utility
yes
0
 
LVL 12

Expert Comment

by:Dustin Saunders
Comment Utility
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 78

Expert Comment

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

Expert Comment

by:Dustin Saunders
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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