Cogger

Segment-01-Asylum.ps1

May 26th, 2022 (edited)
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $version = "Version 2.5.1"
  2. # $ErrorActionPreference = 'SilentlyContinue'
  3.  
  4.  
  5. #=====[ Initial Settings
  6.  
  7. #   Set script name
  8.     $host.ui.RawUI.WindowTitle = "Asylum. $version"
  9.  
  10. #   Hide script from the team member to run this script in the background - Disabled during testing
  11.     $t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
  12.     add-type -name win -member $t -namespace native
  13.     [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
  14.  
  15. #=====[ Load Functions
  16.  
  17.     function ConvertTo-HexString {
  18.         [CmdletBinding()]
  19.         param (
  20.             # Value to convert
  21.             [Parameter(Mandatory=$true, Position = 0, ValueFromPipeline=$true)]
  22.             [object] $InputObjects,
  23.             # Delimiter between Hex pairs
  24.             [Parameter (Mandatory=$false)]
  25.             [string] $Delimiter = ' ',
  26.             # Encoding to use for text strings
  27.             [Parameter (Mandatory=$false)]
  28.             [ValidateSet('Ascii', 'UTF32', 'UTF7', 'UTF8', 'BigEndianUnicode', 'Unicode')]
  29.             [string] $Encoding = 'Default'
  30.         )
  31.  
  32.         begin {
  33.             function Transform ([byte[]]$InputBytes) {
  34.                 [string[]] $outHexString = New-Object string[] $InputBytes.Count
  35.                 for ($iByte = 0; $iByte -lt $InputBytes.Count; $iByte++) {
  36.                     $outHexString[$iByte] = $InputBytes[$iByte].ToString('X2')
  37.                 }
  38.                 return $outHexString -join $Delimiter
  39.             }
  40.  
  41.             ## Create list to capture byte stream from piped input.
  42.             [System.Collections.Generic.List[byte]] $listBytes = New-Object System.Collections.Generic.List[byte]
  43.         }
  44.  
  45.         process
  46.         {
  47.             if ($InputObjects -is [byte[]])
  48.             {
  49.                 Write-Output (Transform $InputObjects)
  50.             }
  51.             else {
  52.                 foreach ($InputObject in $InputObjects) {
  53.                     [byte[]] $InputBytes = $null
  54.                     if ($InputObject -is [byte]) {
  55.                         ## Populate list with byte stream from piped input.
  56.                         if ($listBytes.Count -eq 0) {
  57.                             Write-Verbose 'Creating byte array from byte stream.'
  58.                             Write-Warning ('For better performance when piping a single byte array, use "Write-Output $byteArray -NoEnumerate | {0}".' -f $MyInvocation.MyCommand)
  59.                         }
  60.                         $listBytes.Add($InputObject)
  61.                     }
  62.                     elseif ($InputObject -is [byte[]])
  63.                     {
  64.                         $InputBytes = $InputObject
  65.                     }
  66.                     elseif ($InputObject -is [string])
  67.                     {
  68.                         $InputBytes = [Text.Encoding]::$Encoding.GetBytes($InputObject)
  69.                     }
  70.                     elseif ($InputObject -is [bool] -or $InputObject -is [char] -or $InputObject -is [single] -or $InputObject -is [double] -or $InputObject -is [int16] -or $InputObject -is [int32] -or $InputObject -is [int64] -or $InputObject -is [uint16] -or $InputObject -is [uint32] -or $InputObject -is [uint64])
  71.                     {
  72.                         $InputBytes = [System.BitConverter]::GetBytes($InputObject)
  73.                     }
  74.                     elseif ($InputObject -is [guid])
  75.                     {
  76.                         $InputBytes = $InputObject.ToByteArray()
  77.                     }
  78.                     elseif ($InputObject -is [System.IO.FileSystemInfo])
  79.                     {
  80.                         if ($PSVersionTable.PSVersion -ge [version]'6.0') {
  81.                             $InputBytes = Get-Content $InputObject.FullName -Raw -AsByteStream
  82.                         }
  83.                         else {
  84.                             $InputBytes = Get-Content $InputObject.FullName -Raw -Encoding Byte
  85.                         }
  86.                     }
  87.                     else
  88.                     {
  89.                         ## Non-Terminating Error
  90.                         $Exception = New-Object ArgumentException -ArgumentList ('Cannot convert input of type {0} to Hex string.' -f $InputObject.GetType())
  91.                         Write-Error -Exception $Exception -Category ([System.Management.Automation.ErrorCategory]::ParserError) -CategoryActivity $MyInvocation.MyCommand -ErrorId 'ConvertHexFailureTypeNotSupported' -TargetObject $InputObject
  92.                     }
  93.  
  94.                     if ($null -ne $InputBytes -and $InputBytes.Count -gt 0) {
  95.                         Write-Output (Transform $InputBytes)
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.  
  101.         end {
  102.             ## Output captured byte stream from piped input.
  103.             if ($listBytes.Count -gt 0) {
  104.                 Write-Output (Transform $listBytes.ToArray())
  105.             }
  106.         }
  107.     }
  108.  
  109.     function Set-WindowStyle {
  110.         param(
  111.         [Parameter()]
  112.         [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE',
  113.                      'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED',
  114.                      'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
  115.         $Style = 'SHOW',
  116.         [Parameter()]
  117.         $MainWindowHandle = (Get-Process -Id $pid).MainWindowHandle
  118.         )
  119.         $WindowStates = @{
  120.             FORCEMINIMIZE   = 11; HIDE            = 0
  121.             MAXIMIZE        = 3;  MINIMIZE        = 6
  122.             RESTORE         = 9;  SHOW            = 5
  123.             SHOWDEFAULT     = 10; SHOWMAXIMIZED   = 3
  124.             SHOWMINIMIZED   = 2;  SHOWMINNOACTIVE = 7
  125.             SHOWNA          = 8;  SHOWNOACTIVATE  = 4
  126.             SHOWNORMAL      = 1
  127.         }
  128.         Write-Verbose ("Set Window Style {1} on handle {0}" -f $MainWindowHandle, $($WindowStates[$style]))
  129.  
  130.         $Win32ShowWindowAsync = Add-Type –memberDefinition @
  131.         [DllImport("user32.dll")]
  132.         public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  133. @ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru
  134.  
  135.         $Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[$Style]) | Out-Null
  136.     }
  137.  
  138. #=====[ Load dynamic variables
  139.  
  140. #   Gather Computername
  141.     $ComputerName = $env:COMPUTERNAME
  142.  
  143. #   Gather Username
  144.     $LoggedOnUser = (qwinsta /SERVER:$ComputerName) -replace '\s{2,22}', ',' | ConvertFrom-Csv | Where-Object {$_ -like "*Acti*"} | Select-Object -ExpandProperty USERNAME
  145.     if (!($LoggedOnUser) ) { Write-Host "No user is logged on to $ComputerName" -ForegroundColor Red ; break }
  146.  
  147. #   Gather the Team Members Full Name, instead of First.Last this will read as 'First Last'
  148.     $dom = $env:userdomain
  149.     $usr = $env:username
  150.     $Fullname = ([adsi]"WinNT://$dom/$usr,user").fullname
  151.  
  152. #   Logged in team members document path
  153.     $LoggedPath = "C:\Users\$LoggedOnUser\Documents"
  154.     $OnePath = (Get-ChildItem $LoggedPath -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "OneDrive"}).FullName
  155.    
  156.  
  157.     $ODPathTest = $1DPath -eq "" -or $1DPath -eq $null
  158.     if($ODPathTest -eq $False){ $1DPath = $LoggedPath + "\" + $OnePath }
  159.     if($ODPathTest -eq  $True){ $1DPath = (Get-ChildItem "C:\Users\$LoggedOnUser" -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "OneDrive - "}).FullName }
  160.  
  161.  
  162.  
  163.    
  164.     $1DSoftPath = $1DPath + "\Software"
  165.  
  166. #=====[ Load Scriptblocks
  167. #   Attribution Update
  168.     $atrUdate = { $Cloud_Force_Software = $1DSoftPath
  169.     get-childitem "$Cloud_Force_Software" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {
  170.         attrib.exe $_.fullname +U -P /s
  171.         }
  172.     Start-Sleep -second 7
  173.     }
  174.  
  175. # OneDrive Service
  176. ##  Get the dynamic service name that we need to check, store as variable.
  177.     $OD_DSync = (Get-Service -Name "OneSyncSvc*" -Exclude "OneSyncSvc").Name
  178. ##  Check the status if the dynamically created service, store as variable.
  179.     $OneDriveService = (get-service $OD_DSync | select Status,Can*).Status
  180. ##  Check with boolean logic to see if the service is running, if it isn't running then we will start the service.
  181.     $ODS_Check = $OneDriveService -eq "Stopped"
  182.     if($ODS_Check -eq $True){ Write-Host("OneDrive is $OneDriveService... Starting Service...") ; Start-Service -Name $OD_DSync }
  183.     if($ODS_Check -eq $False){ $null }
  184.  
  185. #=====[ Root directory creation
  186.     #   Create Software directory
  187.     if (Test-Path -Path $1DSoftPath) {
  188.         # Path exists, not action needs to be taken
  189.     } else {
  190.         # Path does not exist, therefore create
  191.         New-Item -Path $1DPath -Name "Software" -ItemType "directory"  -EA SilentlyContinue ; sleep 5
  192.     }
  193.  
  194.  
  195. #=====[ Software list
  196. #   Default Applications of what comes pre-installed beforehand as part of the image.
  197. $STNApp_Array= @(
  198.  
  199.     # Applications from the base
  200.     "Configuration Manager Client"
  201.     "MDOP MBAM"
  202.     "Microsoft InfoPath 2013"
  203.     "Microsoft InfoPath MUI (English) 2013"
  204.     "Microsoft Intune Management Extension"
  205.     "Microsoft Office 32-bit Components 2013"
  206.     "Microsoft Office OSM MUI (English) 2013"
  207.     "Microsoft Office Proofing (English) 2013"
  208.     "Microsoft Office Proofing Tools 2013 - English"
  209.     "Microsoft Office Proofing Tools 2013 - Español"
  210.     "Microsoft Office Shared 32-bit MUI (English) 2013"
  211.     "Microsoft Office Shared MUI (English) 2013"
  212.     "Microsoft Office Shared Setup Metadata MUI (English) 2013"
  213.     "Microsoft Policy Platform"
  214.     "Microsoft Update Health Tools"
  215.     "Microsoft Visual C++ 2022 X64 Additional Runtime - 14.30.30708"
  216.     "Microsoft Visual C++ 2022 X64 Minimum Runtime - 14.30.30708"
  217.     "Microsoft Visual C++ 2022 X86 Additional Runtime - 14.30.30708"
  218.     "Microsoft Visual C++ 2022 X86 Minimum Runtime - 14.30.30708"
  219.     "Office 16 Click-to-Run Extensibility Component"
  220.     "Office 16 Click-to-Run Licensing Component"
  221.     "Outils de vérification linguistique 2013 de Microsoft Office - Français"
  222.     "Teams Machine-Wide Installer"
  223.     # Drivers
  224.     "DisplayLink Graphics"
  225.     "Maxx Audio Installer (x64)"
  226.     "Realtek Audio coM Components"
  227.     "Riverbed SteelHead Mobile"
  228.     # Task Sequence Software Steps
  229.     "1E Client x64"
  230.     "7-Zip 19.00 (x64 edition)"
  231.     "Adobe Acrobat Reader DC"
  232.     "Cisco AnyConnect Secure Mobility Client"
  233.     "Cisco Webex Meetings Desktop App"
  234.     "Anyconnect Profiles 3.6"
  235.     "Google Chrome"
  236.     "Local Administrator Password Solution "
  237.     "ManageEngine Desktop Central - Agent"
  238.     "Microsoft Visio Viewer 2016"
  239.     "UserNotificationConfig"
  240.     "Windows Firewall Configuration Provider"
  241.     "Zscaler"
  242.     )
  243.  
  244. #   Gather installed software on the team members machine
  245.     $INTApp_Array = (get-wmiobject Win32_Product | Sort-Object -Property InstallDate).Name
  246.  
  247. #   Subtract these lists and this gives us what is specificly installed on this machine.
  248.     $App_Array = $INTApp_Array |Where-Object { $STNApp_Array -notcontains $_ }
  249.  
  250. #   Store this software list in OneDrive
  251.     $1DSoftPath_Installed = $1DSoftPath + "\InstalledSoftware.txt"
  252.     echo $App_Array >> $1DSoftPath_Installed
  253.     Invoke-Command -ScriptBlock $atrUdate
  254.  
  255.  
  256.  
  257. #=====[ Outlook encryption certification
  258. #   Search the certificate store for the team members name and gather its thumbprint
  259.     $certThumb = (dir cert: -Recurse | Where-Object { $_.Subject -like "*$Fullname*" } | select-object -first 1).Thumbprint
  260.     ## Commented out next is this same command; but does not look at the first instance, it only looks at unique instances. Might be a good replacement.
  261.     # $certThumb = (dir cert: -Recurse | Where-Object { $_.Subject -like "*$Fullname*" } | Select-object Thumbprint -unique).Thumbprint
  262.  
  263. #   Using this thumbprint we gather the parent paths for both certs that we need to pull for the team member.
  264. ##  * Unsure as code generation if gather both is required as they both use the same thumbprint.
  265.     $CertUserDS = (dir cert: -Recurse | Where-Object { $_.Thumbprint -like "*$certThumb*" } | select-object -first 1).PSParentPath
  266.     $CertMy     = (dir cert: -Recurse | Where-Object { $_.Thumbprint -like "*$certThumb*" } | select-object -last 1).PSParentPath
  267.  
  268. #   Using the janked method of encryption, set dynamic team memember specific variable
  269. #   First.Last is store as a variable
  270.     $CryptCode = $env:username
  271.  
  272. #   Store the first.last variable as an array
  273.     $strArray = $CryptCode.ToCharArray()
  274.  
  275. #   Gather only odd variables from the array
  276.     $strOdd = New-Object System.Collections.ArrayList
  277.     $f=0; $strArray | % { if($f = !$f) { $strOdd.Add("$_") > $null } }
  278.  
  279. #   Gather only even variables from the array
  280.     $strEvn = New-Object System.Collections.ArrayList
  281.     $f=1; $strArray | % { if($f = !$f) { $strEvn.Add("$_") > $null } }
  282.  
  283. #   Split Odd variables from name array to its own array
  284.     $strOdd = $strOdd -replace '(^\s+|\s+$)','' -replace '\s+',' '
  285.     $strOdd = [string]::join("",($strOdd.Split("`n")))
  286.  
  287. #   Split even variables from name array to its own array
  288.     $strEvn = $strEvn -replace '(^\s+|\s+$)','' -replace '\s+',' '
  289.     $strEvn = [string]::join("",($strEvn.Split("`n")))
  290.  
  291. #   Combine Odd and Even variables together to set the first stage of this cipher
  292.     $e_ehx = $strOdd + $strEvn
  293.  
  294. #   Setting the second stage of this cipher, convert the first stage into a hexadecimal.
  295.     $hexconv = ConvertTo-HexString $e_ehx
  296.  
  297. #   Set the final stage of this cipher from hexadecimal to a secure string
  298. ##  Each character in an individuals name will increase the length by 16 characters.
  299. ##  Example. There are 436 characters for a 13 character name
  300. ##  Example. There are 468 characters for a 15 character name
  301.     $pfxEncrypt_Key = $hexconv | ConvertTo-SecureString -AsPlainText -Force
  302.  
  303. #   Certificate  -  cert:\currentuser\my
  304.     $SaveState = $1DSoftPath + "\" + $env:username + "_PKI.pfx"
  305.     Get-ChildItem -Path $CertMy | Export-PfxCertificate -FilePath $SaveState -Password $pfxEncrypt_Key
  306.    
  307.     # IF the pki does not backup for WHATEVER REASON -- This checks and if it does not exist then it uses another method to create the PKI PFX
  308.     $pki_bCheck = [bool](Test-Path -Path $SaveState)
  309.     if($pki_bCheck -eq $False){ certutil -user -p $pfxEncrypt_Key -exportpfx $certThumb $SaveState > $null }
  310.     Start-Sleep -s 7
  311.     $pki_bCheck = [bool](Test-Path -Path $SaveState)
  312.     if($pki_bCheck -eq $False){ $SaveState = $1DSoftPath + "\IMPORT_PKI_FAILED.log" ; New-Item  $SaveState}
  313.     Start-Sleep -s 7
  314.     Invoke-Command -ScriptBlock $atrUdate
  315.  
  316. #=====[ Userprofile data backup and directory attribution update
  317. $1DLibCard = $1DSoftPath + "\Libraries"
  318. $1DLibCardOne = $1DLibCard + "\"
  319. New-Item -Path $1DSoftPath -Name "Libraries" -ItemType "directory" -EA SilentlyContinue
  320. $Library_Array= @(
  321.  
  322. # Profile Library Copy and Attribution
  323. "Desktop"
  324. "Downloads"
  325. "Documents"
  326. "Favorites"
  327. "Pictures"
  328. "Music"
  329. "Temp"
  330. # "Videos"
  331. # ""
  332. )
  333.  
  334.  foreach ($Library in $Library_Array) {
  335.  
  336.  # A simple boolean swtich that we will use to determine the temp directory, and set a constant variable for both
  337.     $LibraryBoolean = [bool]($Library -like "temp")
  338.     if($LibraryBoolean -eq $false){ $locLib = $Env:USERPROFILE + "\" + $Library + "\" }
  339.     if($LibraryBoolean -eq  $true){ $locLib = "C:\temp\" }
  340.  
  341.  # Test directory to see if it exists, if it does then move contents there.
  342.     $1DTestPath = $1DPath + "\" + $Library
  343.     if (Test-Path -Path $1DTestPath) {
  344.         # Path exist, so set this variable as the variable to move files into
  345.         $1DLib = $1DTestPath
  346.     } else {
  347.         # Path doesn't exist.
  348.         # A constant variable that will change with the library
  349.         $1DLib = $1DLibCardOne + $Library
  350.         # Create a new directory based on the library name
  351.         New-Item -Path $1DLibCard -Name $Library -ItemType "directory" -EA SilentlyContinue
  352.     }
  353.  
  354.     # For certain folders we need to check if they are already being synced
  355.     $divBoolean = [bool](($Library -like "Desktop") -or ($Library -like "Pictures") )
  356.     if($divBoolean -eq $false){ <# Nothing needs to be done here, continue like normal. #> }
  357.     if($divBoolean -eq  $true){
  358.  
  359.         $dupeDPath = $1DPath + "\" + $Library + "\"
  360.         $TP_DP = Test-Path -Path $dupeDPath  ;  $TP_lL = Test-Path -Path  $locLib
  361.  
  362.         #   OneDrive Divergent Multiple Duplicate Library Scriptblock
  363.         $Divergent = {
  364.  
  365.             # Gather a cohesive list of all files under the OneDrive Library Path
  366.             $GCI_DDP = Get-ChildItem -Recurse -path $dupeDPath
  367.             # Gather a cohesive list of all files under the User Profile Lirbary Path
  368.             $GCI_LLP = Get-ChildItem -Recurse -path $locLib
  369.  
  370.             # Reduce the length of these variables by one to exclude the last "\""
  371.             $dupeDPath = $dupeDPath.Substring(0,$dupeDPath.Length-1)
  372.             $locLib = $locLib.Substring(0,$locLib.Length-1)
  373.  
  374.             # Compare directories if the files are different in local OneDrive rather than library, copy to the opposite
  375.             Compare-Object $GCI_DDP $GCI_LLP -Property Name, Length  | Where-Object {$_.SideIndicator -eq "<="} | ForEach-Object { Copy-Item "$dupeDPath\$($_.name)" -Destination "$locLib" -Force }
  376.             # Compare directories if the files are different in local library rather than OneDrive, copy to the opposite
  377.             Compare-Object $GCI_DDP $GCI_LLP -Property Name, Length  | Where-Object {$_.SideIndicator -eq "=>"} | ForEach-Object { Copy-Item "$locLib\$($_.name)" -Destination "$dupeDPath" -Force }
  378.             # Compare directories if the files are the same, copy to and from to ensure that the latest of each file is kept.
  379.             Compare-Object $GCI_DDP $GCI_LLP -IncludeEqual -Property Name, Length  | Where-Object {$_.SideIndicator -eq "=="} | ForEach-Object { Copy-Item "$dupeDPath\$($_.name)" -Destination "$locLib" -Force }
  380.             Compare-Object $GCI_DDP $GCI_LLP -IncludeEqual -Property Name, Length  | Where-Object {$_.SideIndicator -eq "=="} | ForEach-Object { Copy-Item "$locLib\$($_.name)" -Destination "$dupeDPath" -Force }
  381.  
  382.             #   Adjust attributes to ensure that these files do not download again on the existing machine
  383.             get-childitem "$dupeDPath" -Depth 99 -Force -File -Recurse | where Attributes -ne '' | foreach {
  384.                 attrib.exe $_.fullname +U -P /s
  385.                 }
  386.  
  387.             #   Adjust attributes to ensure that these files do not download again on the existing machine
  388.             get-childitem "$dupeDPath" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive' | foreach {
  389.                 attrib.exe $_.fullname +U -P /s
  390.                 }
  391.  
  392.             #   Adjust attributes to ensure that these files do not download again on the existing machine
  393.             get-childitem "$dupeDPath" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {
  394.                 attrib.exe $_.fullname +U -P /s
  395.                 }
  396.  
  397.             }
  398.  
  399.         # Only if there exists a folder of each possible duplicate libraries, then run the script block to copy all content to the local library,
  400.         # so during the next stage in the process we can ensure that all files are duplicated properly.
  401.         if(($TP_DP -eq $True) -and ($TP_lL -eq $True)){ Invoke-Command -ScriptBlock $Divergent }
  402.         # Reset the constant variable to ensure that the script can runs as previously designed
  403.         $locLib = $locLib + "\"
  404.     }
  405.  
  406.  # Move contents of all files to OneDrive
  407.     Get-ChildItem $locLib | Where-Object{$_.FullName -notlike "*OneDrive*"} | Copy-Item -Destination $1DLib -Recurse -Force
  408.         Start-Sleep -Seconds 15
  409.  
  410.  #   Adjust attributes to ensure that these files do not download again on the existing machine
  411.     get-childitem "$1DLib" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive' | foreach {
  412.         attrib.exe $_.fullname +U -P /s
  413.         }
  414.  
  415.  #   Adjust attributes to ensure that these files do not download again on the existing machine
  416.     get-childitem "$1DLib" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {
  417.         attrib.exe $_.fullname +U -P /s
  418.         }
  419.  
  420.  }
  421.  
  422.    
  423. #=====[ Application specific task array
  424. #===========[ Chrome - Bookmarks data
  425. #===========[ Network Drives - Drive information and UNC Path
  426. #===========[ Printers - Friendly Names, UNC Path, and driver information
  427. #===========[ Stickey Notes - Database backup
  428. $root_Array= @(
  429.  
  430.    # Software Library Copy and Attribution
  431.     "Chrome"
  432.     "Drives"
  433.     "Printers"
  434.     "Sticky Notes"
  435.     # "Edge"
  436.     # ""
  437.    
  438.     )
  439.  
  440.     foreach ($root in $root_Array) {
  441.  
  442.        # Variable software array based directory creation
  443.         $1D_softPath = $1DSoftPath + "\" + $root
  444.         New-Item -Path $1DSoftPath -Name $root -ItemType "directory"  -EA SilentlyContinue
  445.  
  446.        ##  Script Blocks containing each set of commands appropriate to the task based on the software array
  447.         $sb_Chrome = { ##  Backup Chrome bookmarks
  448.             Copy-Item -Path "$env:userprofile\appdata\local\google\chrome\User Data\default\bookmarks" $1D_softPath
  449.         }
  450.  
  451.         $sb_Drives = {#   Gather all drives and store the Drive letter, the network location, and the friendly name of every network drive.
  452.             $DriveArray = Get-WmiObject -ClassName Win32_MappedLogicalDisk | Select Name, ProviderName, VolumeName
  453.  
  454.         #   For each item in this drive array..
  455.             Foreach ($Drive in $DriveArray) {
  456.  
  457.             # Get each drive letter
  458.             $DriveLetter = ($Drive).Name
  459.             # Get each drive path
  460.             $DrivePath   = ($Drive).ProviderName
  461.             # Get each drive volume name
  462.             $DriveVolume = ($Drive).VolumeName
  463.  
  464.             # Trim the colon from the drive letter
  465.             $DriveLetter = $DriveLetter.Substring(0,$DriveLetter.Length-1)
  466.             # Set variables for dynamic creation
  467.             $SavedDrive = $DriveLetter + ".log"
  468.             $1D_NetDrive = $1D_softPath + "\" + $SavedDrive
  469.  
  470.             # If the drive file exists then we must remove the file to not taint drive information
  471.             if (Test-Path $1D_NetDrive) { Remove-Item $1D_NetDrive }
  472.  
  473.             # Create drive log file and store drive letter as file name
  474.             New-Item -Path $1D_softPath -Name $SavedDrive
  475.             # Store friendly name of drive as first line
  476.             Add-Content -Path $1D_NetDrive -Value "$DriveVolume" -Force
  477.             # Store UNC path of drive as second line
  478.             Add-Content -Path $1D_NetDrive -Value "$DrivePath"   -Force
  479.  
  480.             }
  481.         }
  482.  
  483.         $sb_Printers = {     #   Default Printers of what comes pre-installed beforehand as part of the image.
  484.             $stnPrint_Array= @(
  485.  
  486.             # Default Printer array
  487.             "Local Administrator Password Solution"
  488.             "Webex Document Loader"
  489.             "OneNote (Desktop)"
  490.             "Microsoft XPS Document Writer"
  491.             "Microsoft Print to PDF"
  492.             "Fax"
  493.  
  494.             )
  495.  
  496.             #   Gather printers team members machine
  497.                 $INTPrint_Array = (Get-Printer).Name
  498.  
  499.             #   Subtract these lists and this gives us what printers are on this machine.
  500.                 $Print_Array = $INTPrint_Array |Where-Object { $stnPrint_Array -notcontains $_ }
  501.  
  502.             ForEach ($printer in $Print_Array) {
  503.  
  504.                 $pnt_fName = (Get-Printer -Name $printer).ShareName
  505.                 $pnt_uName = (Get-Printer -Name $printer).Name
  506.                 $pnt_pName = (Get-Printer -Name $printer).PortName
  507.                 $pnt_dName = (Get-Printer -Name $printer).DriverName
  508.                 $pnt_Location = (Get-Printer -Name $printer).Location
  509.                 $pnt_Comment = (Get-Printer -Name $printer).Comment
  510.  
  511.                 $SavedPrinter = $pnt_fName + ".log"
  512.                 $1D_Printer = $1D_softPath + "\" + $SavedPrinter
  513.  
  514.                 # Create drive log file and store drive letter as file name
  515.                 New-Item -Path $1D_softPath -Name $SavedPrinter
  516.  
  517.                 # Store UNC path of printer as first line
  518.                 Add-Content -Path $1D_Printer -Value "$pnt_uName" -Force
  519.  
  520.                 # Store port of printer as second line
  521.                 Add-Content -Path $1D_Printer -Value "$pnt_pName"   -Force
  522.  
  523.                 # Store driver name of printer as third line
  524.                 Add-Content -Path $1D_Printer -Value "$pnt_dName"   -Force
  525.  
  526.                 # Store location of printer as fourth line
  527.                 Add-Content -Path $1D_Printer -Value "$pnt_Location"   -Force
  528.  
  529.                 # Store comment of printer as fifth line
  530.                 Add-Content -Path $1D_Printer -Value "$pnt_Comment"   -Force
  531.  
  532.                 # Add-Content -Path $1D_Printer -Value ""   -Force
  533.  
  534.             }
  535.         }
  536.  
  537.         $sb_StickNote = { #   Location of the existing stickynote archive.
  538.             $stn_sql = $Env:LocalAppData + "\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\plum.sqlite"
  539.             #   Stop Sticky Notes process on the current machine
  540.                 Get-Process | Where-Object {$_.MainWindowTitle -like "*Sticky Notes*"}| Stop-Process
  541.             #   Copy Sticky Notes backup file
  542.                 Copy-Item -Path $stn_sql -Destination $1D_softPath
  543.         }
  544.  
  545.         $sb_Edge = {
  546.             #   If Edge is running then stop this process.
  547.                 Stop-Process -Name "MsEdge"  2>$null
  548.                 $Edge_Stop = Get-Process -Name "MsEdge" 2>$null
  549.                 Stop-Process -InputObject $Edge_Stop  2>$null
  550.                 Get-Process | Where-Object {$_.HasExited}
  551.  
  552.             #   Create backup of Edge Preferences
  553.                 Backup-EdgeProfile -Channel 'Stable' -ExportPath $1D_softPath
  554.                     Start-Sleep -s 3
  555.  
  556.             #   Start Edge after the backup.
  557.                 Start-Process microsoft-edge:
  558.                     sleep 3
  559.                 $wshell = New-Object -ComObject wscript.shell;
  560.                 $wshell.AppActivate('New tab - Work - Microsoft​ Edge')
  561.                     Sleep 2
  562.                 $wshell.SendKeys("^+{T}")
  563.                     sleep 3
  564.                 (Get-Process | Where-Object {$_.MainWindowTitle -like "*Edge*"}).MainWindowHandle | foreach { Set-WindowStyle MINIMIZE $_ }
  565.                     sleep 2
  566.                 $wshell = New-Object -ComObject wscript.shell;
  567.                 $wshell.AppActivate('New tab - Work - Microsoft​ Edge')
  568.                     Sleep 2
  569.                 $wshell.SendKeys("^{W}")
  570.                     sleep 3
  571.                 (Get-Process | Where-Object {$_.MainWindowTitle -like "*Edge*"}).MainWindowHandle | foreach { Set-WindowStyle MAXIMIZE $_ }
  572.         }
  573.  
  574.         # If site equals a specific site then use a specific tool
  575.         switch ($root) {
  576.  
  577.             "Chrome"    { Invoke-Command -ScriptBlock $sb_Chrome    ; break}
  578.             "Drives"    { Invoke-Command -ScriptBlock $sb_Drives    ; break}
  579.             "Printers"  { Invoke-Command -ScriptBlock $sb_Printers  ; break}
  580.             "StickNote" { Invoke-Command -ScriptBlock $sb_StickNote ; break}
  581.             "Edge"      { Invoke-Command -ScriptBlock $sb_Edge      ; break}
  582.             # "banana" { "We found a banana"; break} # template
  583.             default    { "Default" ; break}
  584.             }
  585.  
  586.             Start-Sleep -Seconds 7
  587.  
  588.             #   Adjust Permissions to ensure that these files do not download again on the team members machine
  589.             get-childitem "$1D_softPath" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {
  590.             attrib.exe $_.fullname +U -P /s
  591.         }
  592.  
  593.     }
  594.  
  595.  
  596. #=====[ Attribution alterations command set
  597. #   Adjust Permissions to ensure that these files do not download again on the team members machine
  598.     $Cloud_Force_Software = $1DSoftPath
  599.     get-childitem "$Cloud_Force_Software" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {
  600.         attrib.exe $_.fullname +U -P /s
  601.     }
  602.  
  603.     $attr_Message = "Altering " + "'" + $Cloud_Force_Software + "'" + " folder attributes....."
  604.     Write-Host($attr_Message)
  605.     Start-Sleep -seconds 7
  606.  
  607. #   Adjust Permissions to ensure that these files do not download again on the team members machine
  608.     $Cloud_Force_Libraries = $1DLibCard
  609.     get-childitem "$Cloud_Force_Libraries" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {
  610.         attrib.exe $_.fullname +U -P /s
  611.     }
  612.  
  613. #=====[ Final directory attribution set
  614.     #   Adjust Permissions to ensure that these files do not download again on the team members machine
  615.         $Cloud_Force_Libraries = $1DSoftPath
  616.         get-childitem "$Cloud_Force_Libraries" -Depth 99 -Force -File -Recurse | where Attributes -eq 'Archive, ReparsePoint' | foreach {
  617.             attrib.exe $_.fullname +U -P /s
  618.         }
  619.  
Add Comment
Please, Sign In to add comment