?
Solved

Powershell script explanation

Posted on 2016-10-04
10
Medium Priority
?
87 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
[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
  • 4
10 Comments
 
LVL 13

Accepted Solution

by:
Dustin Saunders earned 2000 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 13

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
Bringing Advanced Authentication to the SMB Market

WatchGuard announces the acquisition of advanced authentication provider, Datablink, with one mission – to bring secure authentication to SMB, mid-market, and distributed enterprises with a cloud-based solution, ideal for resale via their established channel & MSSP community.

 
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 13

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 13

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 82

Expert Comment

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
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…
Suggested Courses

762 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