Victor G. Brusca
SOFTWARE DEVELOPER
SYSTEM ADMINISTRATOR
DATABASE ADMINISTRATOR

Contact

XXX.XXX.XXXX

XX XXX XX.

XXX. XX
XXXXXX XX, XXXXX

vacuous_b@yahoo.com

 


PowerShell Scripts

Main PowerShell Script - Provides support for database backups, web site backups, time-stamping, web.config management, distributed SQL query executing for ease of use server synchronization during some maintenance tasks, web site publishing to test and prod, web service publishing to test and prod.
** Important server names, IP addresses, and passwords have been remitted and replaced with tags, [A TAG].

PowerShell Script Archive

Add-Type -AssemblyName System.IO
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Pscx.dll") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null

$wwwRoot = "C:\inetpub\wwwroot\"
$wwwDir = "C:\inetpub\wwwroot\[SITE ROOT]\"
$wwwSvcDir = "C:\inetpub\wwwroot\[SITE ROOT]Svc\"
$wwwScratch = "C:\inetpub\wwwroot\scratch\"

$wwwPubOrig = "\\[TEST SERVER]\Share\Victor"
$wwwPub = "Z:"

$password = "[YOUR PASSWORD]"
$username = "[MY SERVER]\Victor"

function Dir-Check($path) 
{
   [System.IO.Directory]::Exists($path)
}

function File-Check($path)
{
    [System.IO.File]::Exists($path)
}

function Dir-Create($path)
{
    if ([bool](Dir-Check $path) -eq $false)
    {
        [System.IO.Directory]::CreateDirectory($path)
        return $true
    }
    else
    {
        return $false
    }
}

function Dir-Copy($from, $to)
{
    if([bool](Dir-Check ($to + (Dir-Name $from))) -eq $true)
    {
        "Destination directory already exists."
        return $false;
    }
    elseif ([bool](Dir-Check $to) -eq $true -and [bool](Dir-Check $from) -eq $true)
    {
        try
        {
            Copy-Item -Path $from -Destination $to -Recurse 
            return $true
        }
        catch
        {
            "Error copying directory!"
            return $false
        }
    }
    else
    {
        "Source or destination dir doesn't exist."
        return $false
    }
}

function Dir-Name($dir)
{
    $obj = New-Object -TypeName System.IO.DirectoryInfo -ArgumentList $dir
    return $obj.Name
}

function File-Dir-Name($file)
{
    $obj = New-Object -TypeName System.IO.FileInfo -ArgumentList $file
    return $obj.DirectoryName
}

function Map-Repo
{
    try
    {
        $net = New-Object -com WScript.Network
        $drive = "Z:"
        $path = $wwwPubOrig
    
        if (test-path $drive) 
        { 
            #do nothing
        }
        else
        {
            $net.MapNetworkDrive($drive, $path, $true, $username, $password)
        }
    }
    catch
    {
        "Inoring: " + $_
    }
}

function Unmap-Repo
{
    try
    {
        $net = New-Object -com WScript.Network
        $drive = "Z:"
        
        <#
        if(Test-Path -Path "HKCU:\Network\Z")
        {
            Remove-Item -Path "HKCU:\Network\Z" -Recurse -Force
        }
        #>

        if (test-path $drive) 
        { 
            $net.RemoveNetworkDrive($drive)
        }
    }
    catch
    {
        "Inoring: " + $_
    }
}

function Open-Repo
{
    ii $wwwPub
}

function Open-Fsi
{
    ii "C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0\fsi.exe"
}

function Open-Profile
{
    notepad $profile
}

function P4s-Help
{
    ""
    ""
    "Run P4s-Help for this command list..."
    "------Database Backup Commands------"
    "Backup dev PLAN4SAFETY on [DEV SERVER] with: Run-Backup-Db-Dev"
    "Backup test PLAN4SAFETY on [TEST SERVER] with: Run-Backup-Db-Test"
    "Backup prod PLAN4SAFETY on TSR2014D with: Run-Backup-Db-Prod1"
    "Backup prod PLAN4SAFETY on TSR2014B with: Run-Backup-Db-Prod2"
    ""
    ""
    "------Website Backup Commands------"
    "Backup dev [SITE ROOT], [SITE ROOT]Svc on [DEV SERVER] with: Run-Repo-Dev-All"
    "Backup test [SITE ROOT], [SITE ROOT]Svc on [TEST SERVER] with: Run-Repo-Test-All"
    "Backup prod [SITE ROOT], [SITE ROOT]Svc on [PROD SERVER] with: Run-Repo-Prod-All"
    ""
    ""
    "------Website Publish Commands------"
    "Publish a build to test, [TEST SERVER] with: Run-Pub-Test-All 'dd-mm-yyyy'"
    "Publish a build to prod, [PROD SERVER] with: Run-Pub-Prod-All 'dd-mm-yyyy'"
    ""
    ""
    "------Daily Work Commands------"
    "Backup and publish all new sites and databases with: Run-Daily-Work"
    "Backup and publish all new sites with: Run-Daily-Work-Code"
    ""
    ""
}
P4s-Help

function Run-Daily-Work-Code
{
    Run-Repo-All; 
    Run-Pub-Test-All-Today; 
    Run-Pub-Prod-All-Today;
}

function Run-Daily-Work
{
    Run-Dev-Push; 
    Run-Backup-Db-Prod1; 
    Run-Backup-Db-Prod2; 
    Run-Backup-Db-Test; 
    Run-Pub-Test-All-Today; 
    Run-Pub-Prod-All-Today;
}

function Check-P4s3-Usps($dbName, $instances)
{
    $db = new-object(‘Microsoft.SqlServer.Management.Smo.Server’) "LOCALHOST"
    $cUsps = $db.Databases[$dbName].StoredProcedures | Where {$_.Schema -eq "dbo"}
    #$cUsps | Out-GridView

    foreach($instance in $instances)
    {
        if($instance -eq "[DEV SERVER]")
        {
            ""
            ""
            $dbName
            $instance

            $db = new-object(‘Microsoft.SqlServer.Management.Smo.Server’) "LOCALHOST"
            $usps = $db.Databases[$dbName].StoredProcedures | Where {$_.Schema -eq "dbo"}
            "Found " + $usps.Count + " on " + $instance

            $mis = 0
            foreach($t in $cUsps)
            {
                $res = $usps | Where {$_.Name -eq $t.Name}
                if($res.Count -eq 0)
                {
                    $dbName + " is missing stored procedure " + $t.Name + "."
                    $mis++
                }
            }

            if($mis -eq 0)
            {
                $dbName + " has all stored procedures."
            }
        }
        else
        {
            $testName = $instance
            $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
            $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

            $sBlock = {
                param
                (
                    $lDbName,
                    $lInstance,
                    $lUspList
                )

                ""
                ""
                $lDbName
                $lInstance

                if($lInstance -eq "[TEST SERVER]")
                {
                    . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
                    Map-Repo
                    Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Test.ps1" -Destination "C:\Users\Victor\Desktop\" -Force
                    . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1

                    $lDbName = "P4S3"
                    $db = new-object(‘Microsoft.SqlServer.Management.Smo.Server’) "LOCALHOST\tsrcdb"
                    $usps = $db.Databases[$lDbName].StoredProcedures | Where {$_.Schema -eq "dbo"}
                }
                else
                {
                    . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
                    Map-Repo
                    Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
                    . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1

                    $db = new-object(‘Microsoft.SqlServer.Management.Smo.Server’) "LOCALHOST\tsrcdb"
                    $usps = $db.Databases[$lDbName].StoredProcedures | Where {$_.Schema -eq "dbo"}
                }

                "Found " + $usps.Count + " on " + $lInstance

                $mis = 0
                foreach($t in $lUspList)
                {
                    $res = $usps | Where {$_.Name -eq $t.Name}
                    if($res.Count -eq 0)
                    {
                        $lDbName + " is missing stored procedure " + $t.Name + "."
                        $mis++
                    }
                }

                if($mis -eq 0)
                {
                    $lDbName + " has all stored procedures."
                }
            }

            $s = New-PSSession -ComputerName $testName -Credential $c            
            Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList $dbName,$instance,$cUsps
            Remove-PSSession $s.Id
        }
    }
}

function Run-Sql-On-All-P4s($shareFile)
{
    Run-Sql-On-Dev-P4s $shareFile
    Run-Sql-On-Test-P4s $shareFile
    Run-Sql-On-Prod1-P4s $shareFile
    Run-Sql-On-Prod2-P4s $shareFile
}

function Run-Sql-On-Dev-P4s($shareFile)
{
    $target = $wwwPub + "\dev_tools\shared_sql\" + $shareFile

    "Looking for sql file: " + $target

    $t = $dbServerDict[0]

    Map-Repo

    if(-not (Test-Path $target))
    {
        "Could not find shared sql file: " + $target
        return
    }
    
    "Running SQL against " + $t.Dbs[0] + " on server " + $t.Server + "."
    ""
    #Get-Content $target
    ""
    invoke-sqlcmd -inputfile $target -serverinstance $t.Instance -database $t.Dbs[0]
}

function Run-Sql-On-Test-P4s($shareFile)
{
    $target = $wwwPub + "\dev_tools\shared_sql\" + $shareFile

    "Looking for sql file: " + $target

    $t = $dbServerDict[1]

    Map-Repo

    if(-not (Test-Path $target))
    {
        "Could not find shared sql file: " + $target
        return
    }

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            $lDbName,
            $lTarget,
            $lInstance,
            $lServer
        )
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Test.ps1" -Destination "C:\Users\Victor\Desktop\" -Force
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        
        "Running SQL against " + $lDbName + " on server " + $lServer + "."
        ""
        #Get-Content $lTarget
        ""
        invoke-sqlcmd -inputfile $lTarget -serverinstance $lInstance -database $lDbName
    }

    $s = New-PSSession -ComputerName $t.Server -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList $t.Dbs[0],$target,$t.Instance,$t.Server
    Remove-PSSession $s.Id
}

function Run-Sql-On-Prod1-P4s($shareFile)
{
    $target = $wwwPub + "\dev_tools\shared_sql\" + $shareFile

    "Looking for sql file: " + $target

    $t = $dbServerDict[2]

    Map-Repo

    if(-not (Test-Path $target))
    {
        "Could not find shared sql file: " + $target
        return
    }

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            $lDbName,
            $lTarget,
            $lInstance,
            $lServer
        )
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        
        "Running SQL against " + $lDbName + " on server " + $lServer + "."
        ""
        #Get-Content $lTarget
        ""
        invoke-sqlcmd -inputfile $lTarget -serverinstance $lInstance -database $lDbName
    }

    $s = New-PSSession -ComputerName $t.Server -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList $t.Dbs[0],$target,$t.Instance,$t.Server
    Remove-PSSession $s.Id
}

function Run-Sql-On-Prod2-P4s($shareFile)
{
    $target = $wwwPub + "\dev_tools\shared_sql\" + $shareFile

    "Looking for sql file: " + $target

    $t = $dbServerDict[3]

    Map-Repo

    if(-not (Test-Path $target))
    {
        "Could not find shared sql file: " + $target
        return
    }

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            $lDbName,
            $lTarget,
            $lInstance,
            $lServer
        )
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        
        "Running SQL against " + $lDbName + " on server " + $lServer + "."
        ""
        #Get-Content $lTarget
        ""
        invoke-sqlcmd -inputfile $lTarget -serverinstance $lInstance -database $lDbName
    }

    $s = New-PSSession -ComputerName $t.Server -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList $t.Dbs[0],$target,$t.Instance,$t.Server
    Remove-PSSession $s.Id
}

function Run-Backup-Db-Dev
{    
   Backup-Db "PLAN4SAFETY" "C:\temp" "dev" "LOCALHOST"
}

function Run-Backup-Db-Test
{
    $testName = "[TEST SERVER]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            $lDbName,
            $lDir,
            $lSrc,
            $lInstance
        )
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Test.ps1" -Destination "C:\Users\Victor\Desktop\" -Force
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Backup-Db $lDbName $lDir $lSrc $lInstance
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList "PLAN4SAFETY","D:\P4S3\Scratch","test","LOCALHOST\TSRCDB"
    Remove-PSSession $s.Id
}

function Run-Backup-Db-Prod1
{
    $testName = "[PROD DB]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            $lDbName,
            $lDir,
            $lSrc,
            $lInstance
        )
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Backup-Db $lDbName $lDir $lSrc $lInstance
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList "PLAN4SAFETY","C:\temp","prod1","LOCALHOST\TSRCDB"
    Remove-PSSession $s.Id
}

function Run-Backup-Db-Prod2
{
    $testName = "[PROD DB BAK]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            $lDbName,
            $lDir,
            $lSrc,
            $lInstance
        )
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Backup-Db $lDbName $lDir $lSrc $lInstance
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList "PLAN4SAFETY","C:\temp","prod2","LOCALHOST\TSRCDB"
    Remove-PSSession $s.Id
}

function Backup-Db($dbName, $dir, $src, $instance)
{
    if(-not $dbName)
    {
        "No database name found."
        return
    }

    if(-not $dir)
    {
        "No directory name found."
        return
    }
    else
    {
        if(-not (test-path $dir))
        {
            New-Item -Path $dir -ItemType directory -Force
        }
    }


    if(-not $src)
    {
        "No src [dev, test, prod] found."
        return
    }

    $s = new-object ("Microsoft.SqlServer.Management.Smo.Server") $instance

    $dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
    $dbBackup.Database = $dbName
    
    $bak = $dir + "\" + $src + "_" + (Ps-Local-Pc) + "_" + $dbName + "_db_" + (Get-Date -Format MM-dd-yyyy) + ".bak"
    $zBak = $dir + "\" + $src + "_" + (Ps-Local-Pc) + "_" + $dbName + "_db_" + (Get-Date -Format MM-dd-yyyy) + ".zip"

    if(-not (test-path $bak))
    {
        new-item -path $bak -itemtype file -force
    }

    try
    {
        $dbBackup.Devices.AddDevice($bak, "File")
        $dbBackup.Action = "Database"
        $dbBackup.MediaDescription = "Disk"
        $dbBackup.SqlBackup($s)
    }
    catch
    {
        "Error caught: $($error[0])"
        return
    }

    Write-Zip $bak $zBak
    Remove-Item $bak -Force
    Map-Repo

    $destZip = $wwwPub + "\" + $src + "_" + (Ps-Local-Pc) + "_" + $dbName + "_db_" + (Get-Date -Format MM-dd-yyyy) + ".zip"
    if(test-path $destZip)
    {
        Remove-Item $destZip -Force
    }

    Move-Item $zBak $wwwPub
}

function Ps-List-Aliases
{
    $aliases
}

function Ps-Backup-Profile($src)
{
    Map-Repo

    if(-not $src)
    {
        "No src [dev, test, prod] found defaulting to dev."
        $src = "dev"
    }

    $dest = $wwwPub + "\" + $src + "_" + (Ps-Local-Pc) + "_" + $env:USERNAME + "_ps_profile_" + (Get-Date -Format MM-dd-yyyy) + ".zip"
    
    "Backing up PS profile: " + (File-Dir-Name $profile) + " to " + $dest

    if(Test-Path $dest)
    {
        Remove-Item $dest -Force
    }

    Write-Zip (File-Dir-Name $profile) $dest
}

function Ps-Create-Profile
{
    if(-not (test-path $profile))
    {
        new-item -path $profile -itemtype file -force
    }
    notepad $profile
}

function Ps-Version
{
    $PSVersionTable
}

function Ps-Local-Pc
{
    $env:COMPUTERNAME
}

function Ps-Get-Trusted
{
    get-item wsman:localhost\client\trustedhosts
    $t = get-item wsman:localhost\client\trustedhosts
    $t.Value
}

function Ps-Set-Trusted
{
    set-item wsman:localhost\client\trustedhosts -value "[TEST SERVER],[PROD SERVER],[PROD DB BAK],[PROD DB]" -force
}

function Ps-Open-Sessions
{
    Get-PSSession
}

function Repo-Dev-[SITE ROOT]Svc
{
    Repo-[SITE ROOT]Svc "dev" $true
}

function Repo-Test-[SITE ROOT]Svc
{
    Repo-[SITE ROOT]Svc "test" $false
}

function Repo-Prod-[SITE ROOT]Svc
{
    Repo-[SITE ROOT]Svc "prod" $false
}

function Repo-[SITE ROOT]Svc($prefix, $strip)
{
    $zFileName = $prefix + "_" + (Dir-Name $wwwSvcDir) + "_" + (Get-Date -Format MM-dd-yyyy) + ".zip"
    $zName = $wwwScratch + $zFileName
    $srcDir = $wwwScratch + (Dir-Name $wwwSvcDir) + "\"

    Dir-Create $wwwScratch

    if([bool](Dir-Check $srcDir) -eq $true)
    {
        Remove-Item $srcDir -Recurse -Force
    }

    "Source: " + $wwwSvcDir
    "Destination: " + $wwwScratch
    Dir-Copy $wwwSvcDir $wwwScratch
    
    if([bool](File-Check $zName) -eq $true)
    {
        Remove-Item $zName -Force
    }
    
    ("Strip: " + ($srcDir + "aspnet_client\"))
    Remove-Item -Recurse ($srcDir + "aspnet_client\") -Force

    if($strip)
    {
        ("Strip: " + ($srcDir + "web.config"))
        Remove-Item ($srcDir + "web.config") -Force
    }

    "Zip Source: " + $srcDir
    Write-Zip $srcDir $zName
    Map-Repo
    Move-Item $zName ($wwwPub + "\" + $zFileName) -Force
    Remove-Item $srcDir -Recurse -Force
}

function Repo-Dev-[SITE ROOT]
{
    Repo-[SITE ROOT] "dev" $true
}

function Repo-Test-[SITE ROOT]
{
    Repo-[SITE ROOT] "test" $false
}

function Repo-Prod-[SITE ROOT]
{
    Repo-[SITE ROOT] "prod" $false
}

function Repo-[SITE ROOT]($prefix, $strip)
{
    $zFileName = $prefix + "_" + (Dir-Name $wwwDir) + "_" + (Get-Date -Format MM-dd-yyyy) + ".zip"
    $zName = $wwwScratch + $zFileName
    $srcDir = $wwwScratch + (Dir-Name $wwwDir) + "\"

    Dir-Create $wwwScratch

    if([bool](Dir-Check $srcDir) -eq $true)
    {
        Remove-Item $srcDir -Recurse -Force
    }

    "Source: " + $wwwDir
    "Destination: " + $wwwScratch
    Dir-Copy $wwwDir $wwwScratch
    
    if([bool](File-Check $zName) -eq $true)
    {
        Remove-Item $zName -Force
    }
    
    ("Strip: " + ($srcDir + "aspnet_client\"))
    Remove-Item -Recurse ($srcDir + "aspnet_client\") -Force

    ("Strip: " + ($srcDir + "temp\"))
    Remove-Item -Recurse ($srcDir + "temp\") -Force

    if($strip)
    {
        ("Strip: " + ($srcDir + "web.config"))
        Remove-Item ($srcDir + "web.config") -Force
    }

    "Zip Source: " + $srcDir
    Write-Zip $srcDir $zName
    Map-Repo
    Move-Item $zName ($wwwPub + "\" + $zFileName) -Force
    Remove-Item $srcDir -Recurse -Force
}

function Repo-Dev-All
{
    Repo-Dev-[SITE ROOT]
    Repo-Dev-[SITE ROOT]Svc
}

function Repo-Test-All
{
    Repo-Test-[SITE ROOT]
    Repo-Test-[SITE ROOT]Svc
}

function Repo-Prod-All
{
    Repo-Prod-[SITE ROOT]
    Repo-Prod-[SITE ROOT]Svc
}

function Run-Repo-Dev-All
{
    Repo-Dev-All
}

function Run-Repo-Test-All
{
    $testName = "[TEST SERVER]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Test.ps1" -Destination "C:\Users\victor\Desktop\" -Force
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Repo-Test-All
    }

    #$c = Get-Credential
    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock
    Remove-PSSession $s.Id
}

function Pub-Test-All($ds)
{
    $wr = $wwwRoot
    $ws = $wwwScratch
    $archiveName = "dev_[SITE ROOT]_" + ($ds) + ".zip"
    $archive = "\\[TEST SERVER]\Share\Victor\" + $archiveName
    $scratchArchive = $ws + $archiveName

    "Archive: " + $archive
    "Scratch Archive: " + $scratchArchive

    if(Test-Path $archive)
    {
        "Found Archive: " + $archive
        Copy-Item $archive $ws -Force
        Expand-Archive $scratchArchive $wr -Force
        Remove-Item $scratchArchive -Force
    }
    else
    {
        "Could not find target DEV [SITE ROOT] archive: " + $archive
    }

    $archiveName = "dev_[SITE ROOT]Svc_" + ($ds) + ".zip"
    $archive = "\\[TEST SERVER]\Share\Victor\" + $archiveName
    $scratchArchive = $ws + $archiveName

    "Archive: " + $archive
    "Scratch Archive: " + $scratchArchive

    if(Test-Path $archive)
    {
        "Found Archive: " + $scratchArchive
        Copy-Item $archive $ws -Force
        Expand-Archive $scratchArchive $wr -Force
        Remove-Item $scratchArchive -Force
    }  
    else
    {
        "Could not find target DEV [SITE ROOT]Svc archive: " + $archive
    }
}

function Run-Pub-Test-All-Today
{
    Run-Pub-Test-All (Get-Date -Format MM-dd-yyyy)
}

function Run-Pub-Prod-All-Today
{
    Run-Pub-Prod-All (Get-Date -Format MM-dd-yyyy)
}

function Run-Pub-Test-All($dateString)
{
    if($dateString.Length -eq 0)
    {
        "Valid date string is required: MM-dd-YYYY"
        return
    }

    $testName = "[TEST SERVER]";
    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            [string]$ds
        )

        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Test.ps1" -Destination "C:\Users\victor\Desktop\" -Force
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Pub-Test-All $ds
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList $dateString
    Remove-PSSession $s.Id
}

function Run-Pub-Prod-All($dateString)
{
    if($dateString.Length -eq 0)
    {
        "Valid date string is required: MM-dd-YYYY"
        return
    }

    $testName = "[PROD SERVER]";
    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        param
        (
            [string]$ds
        )

        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Pub-Test-All $ds
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock -ArgumentList $dateString
    Remove-PSSession $s.Id
}

function Update-Test-Script
{
    $testName = "[TEST SERVER]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        . C:\Users\victor\Desktop\P4s3-Deploy-Test.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Test.ps1" -Destination "C:\Users\victor\Desktop\" -Force
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock
    Remove-PSSession $s.Id
}

function Run-Repo-Prod-All
{
    $testName = "[PROD SERVER]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Repo-Prod-All
    }

    #$c = Get-Credential
    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock
    Remove-PSSession $s.Id
}

function Update-Prod-Script
{
    $testName = "[PROD SERVER]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock
    Remove-PSSession $s.Id
}

function Update-Db-Prod1-Script
{
    $testName = "[PROD DB BAK]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock
    Remove-PSSession $s.Id
}

function Update-Db-Prod2-Script
{
    $testName = "[PROD DB]";

    $secpasswd = ConvertTo-SecureString "[YOUR PASSWORD]" -AsPlainText -Force
    $c = New-Object System.Management.Automation.PSCredential ("Administrator", $secpasswd)

    $sBlock = {
        . C:\Users\Administrator\Desktop\P4s3-Deploy-Prod.ps1
        Map-Repo
        Copy-Item -Path "\\[TEST SERVER]\Share\Victor\dev_tools\power_shell_scripts\P4s3-Deploy-Prod.ps1" -Destination "C:\Users\Administrator\Desktop\" -Force
    }

    $s = New-PSSession -ComputerName $testName -Credential $c
    Invoke-Command -Session $s -ScriptBlock $sBlock
    Remove-PSSession $s.Id
}

function Run-Repo-All
{
    Run-Repo-Dev-All
    Run-Repo-Test-All
    Run-Repo-Prod-All
}

function Update-All-Scripts
{
    Update-Test-Script
    Update-Prod-Script
    Update-Db-Prod1-Script
    Update-Db-Prod2-Script
}

function Run-Dev-Push
{
    Run-Repo-All
    Run-Backup-Db-Dev
}
                                                    


Batch File and PowerShell Script - A windows cmd file that can be run as a scheduled task pointing to a simple PowerShell script that loads the main script into memory and executes a method. This version runs the "ALL" option which backups all databases before pushing out new code to all servers.

PowerShell Execution All Cmd

powershell -command "& 'C:\Users\Vbrusca\Documents\files\work\power_shell_scripts\site_publish_all.ps1' "
                                                    


. C:\Users\Vbrusca\Documents\files\work\power_shell_scripts\P4s3-Deploy.ps1
Run-Daily-Work
                                                    


Batch File and PowerShell Script - A windows cmd file that can be run as a scheduled task pointing to a simple PowerShell script that loads the main script into memory and executes a method. This version runs the "CODE" option which pushes code out to all servers and does not run a batabase backup.

PowerShell Execution Code Only Cmd

powershell -command "& 'C:\Users\Vbrusca\Documents\files\work\power_shell_scripts\site_publish_code.ps1' "
                                                    


. C:\Users\Vbrusca\Documents\files\work\power_shell_scripts\P4s3-Deploy.ps1
Run-Daily-Work-Code