$pass = ConvertTo-SecureString ("Paper123" + [char]33 + "@#") -AsPlainText -Force $cred = New-Object PSCredential("INTRANET\sysadmin", $pass) $uncRoot = "\\192.168.0.6\C$" New-PSDrive -Name AD2 -PSProvider FileSystem -Root $uncRoot -Credential $cred -ErrorAction Stop | Out-Null # Write a one-time bulk sync runner script $bulkScript = @" # One-time bulk sync with 86400 minute window (60 days) # This catches all stranded files from the last ~35 days # Auto-deletes itself after running Set-Location "C:\Shares\test\scripts" & powershell -NoProfile -ExecutionPolicy Bypass -File "C:\Shares\test\scripts\Sync-FromNAS.ps1" -MaxAgeMinutes 86400 -Verbose # Log completion Add-Content -Path "C:\Shares\test\scripts\sync-from-nas.log" -Value "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) : BULK SYNC COMPLETE - one-time catchup finished" # Clean up this trigger script Remove-Item -Path "C:\Shares\test\scripts\BulkSync-OneTime.ps1" -Force -ErrorAction SilentlyContinue "@ Set-Content -Path "AD2:\Shares\test\scripts\BulkSync-OneTime.ps1" -Value $bulkScript Write-Output "[OK] Bulk sync script written to AD2:\Shares\test\scripts\BulkSync-OneTime.ps1" # Now create a scheduled task on AD2 to run it immediately # We can do this by writing a schtasks command file $taskScript = @" schtasks /create /tn "BulkSync-OneTime" /tr "powershell -NoProfile -ExecutionPolicy Bypass -File C:\Shares\test\scripts\BulkSync-OneTime.ps1" /sc once /st 00:00 /ru INTRANET\sysadmin /rp "Paper123\!@#" /f schtasks /run /tn "BulkSync-OneTime" "@ Set-Content -Path "AD2:\Shares\test\scripts\run-bulk.cmd" -Value $taskScript Write-Output "[OK] Task creation script written" Write-Output "" Write-Output "NOTE: The scheduled task needs to be triggered on AD2." Write-Output "Attempting to use WMI to create process on AD2..." # Try WMI process creation (may work even if WinRM doesnt) try { $proc = Invoke-WmiMethod -ComputerName 192.168.0.6 -Credential $cred -Class Win32_Process -Name Create -ArgumentList "powershell -NoProfile -ExecutionPolicy Bypass -File C:\Shares\test\scripts\BulkSync-OneTime.ps1" if ($proc.ReturnValue -eq 0) { Write-Output "[OK] WMI process started on AD2\! PID: $($proc.ProcessId)" } else { Write-Output "[ERROR] WMI process creation failed with return value: $($proc.ReturnValue)" } } catch { Write-Output "[ERROR] WMI failed: $_" Write-Output "Trying schtasks approach..." try { $schResult = schtasks /create /s 192.168.0.6 /u INTRANET\sysadmin /p "Paper123\!@#" /tn "BulkSync-OneTime" /tr "powershell -NoProfile -ExecutionPolicy Bypass -File C:\Shares\test\scripts\BulkSync-OneTime.ps1" /sc once /st 00:00 /ru INTRANET\sysadmin /rp "Paper123\!@#" /f 2>&1 Write-Output "schtasks create: $schResult" $runResult = schtasks /run /s 192.168.0.6 /u INTRANET\sysadmin /p "Paper123\!@#" /tn "BulkSync-OneTime" 2>&1 Write-Output "schtasks run: $runResult" } catch { Write-Output "[ERROR] schtasks also failed: $_" } } Remove-PSDrive -Name AD2 -ErrorAction SilentlyContinue