Avatar of Leadtheway
Leadtheway
Flag for United States of America asked on

Powershell script explanation

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

PowershellShell Scripting

Avatar of undefined
Last Comment
Leadtheway

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Dustin Saunders

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Leadtheway

ASKER
where would i look to see if it's running as a service?
Dustin Saunders

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.
Leadtheway

ASKER
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Dustin Saunders

Is this still a valid location?
$dstDir = "\\tc00afsv02\c$\Shares\zvol2$\OpexTest\Unprocessed"
Leadtheway

ASKER
yes
Dustin Saunders

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Johnson, CD

It won't be running as a service but a scheduled task
Dustin Saunders

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.
Leadtheway

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23