Clayton Lewis
Clayton Lewis

Reputation: 394

Logging to network share not working via PS post reboot at startup

Getting ready to use a few scripts that I have had written for a while but was never able to solve one particular issue. Hoping to find a solution.

The project is to do a Win10 Build upgrade to around 900 machines in one weekend. Need to ensure they are back up in time and that the upgrade was successful.

Files are staged on a file share. Scripts are stored on the same share. A task is going to created with a bat file on each machine to go get the scripts and execute them.

In the scripts I have it set to record data and its progress to its own file on the network with the name of the file being computer.txt

The Issue:

After the upgrade finishes rebooting it will execute a task that is set to run 'AtStartup'. I have it reconnect to the network share immediately to start logging its status again. It will sometimes work perfectly fine and other times it will not. 50/50. The script works aside from this. Everything finishes fine.

Why the logging?

There are a lot of computers to watch all at one time with a limited number of people to watch over them and address issues. The logs will let me know where there might be a possible issue with how I have it logging. I would like to continue to get the data post reboots.

It doesn't appear to be a network issue because I have verified the machine is communicating.

Ive tried adding a sleep timer to give Windows 2 minutes to finish booting. That didn't help.

I am not sure where to look to find why it works only sometimes.

Mapping of Network Share Path and setting file variable

$Drive_Root_Path = "\\File Path\"
$Drive_Letter = "X"
Remove-PSDrive $Drive_Letter
$Drive = New-PSDrive -Name $Drive_Letter -PSProvider FileSystem -Root $Drive_Root_Path

$Win10_Upgrade_Log_Folder = $Drive.Root + "Log Folder\"
$Log_File = $Win10_Upgrade_Log_Folder + $env:COMPUTERNAME + ".txt"

Example how data is added to the file

Add-Content -Path $Log_File -Value (Get-Date) -NoNewline
Add-Content -Path $Log_File -Value " Setting Executino Policy back to Restricted."

I know there are better solutions to upgrade Windows but with the options available to me, I had to come up with something. It works minus the part of logging after reboot.

Upvotes: 0

Views: 120

Answers (1)

HAL9256
HAL9256

Reputation: 13523

I agree with @Theo to try using a UNC path instead of a mapped drive. I also advocate to try also using fully qualified domain names in the path. This makes your code even simpler:

$Win10_Upgrade_Log_Folder = "\\server.contoso.com\Log Folder\"
$Log_File = $Win10_Upgrade_Log_Folder + $env:COMPUTERNAME + ".txt"

And adding content is the same:

Add-Content -Path $Log_File -Value (Get-Date) -NoNewline
Add-Content -Path $Log_File -Value " Setting Executino Policy back to Restricted."

Upvotes: 1

Related Questions