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

x
?
Solved

Powershell script explanation

Posted on 2016-10-04
10
Medium Priority
?
95 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 83

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…

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