I'm currently trying to pull msinfo data from a remote server and then save that output onto a share located on another server. When I run the command, a progress bar appears and then completes without apparent issue, but the file isn't saved to the UNC path. I've verified that I have permissions on the share and that the nfo generation itself works. Any ideas?
C:\Windows\system32>msinfo32 /computer servername /nfo \\sharename\filename.nfo
Very Strange, It works on CMD but not with Powershell, Not had the time to explore it, However If you need to run it in powershell you can take this workaround:
$TempFile = [System.IO.Path]::GetTempFileName()
C:\Windows\system32\msinfo32 /computer Computer /nfo $TempFile
Do
{
Sleep 5
}
Until (!(Get-Process msinfo32 -ErrorAction SilentlyContinue))
Copy-Item $TempFile \\Computer\Share\output.nfo
$TempFile | Remove-Item -Force
Got it figured out - I was able to use the switch user param to save the file after pulling it from the server.
Thanks!
Related
New to scripting, currently trying to write a script that will Invoke command to every computer located within domain. My issue is I am trying to direct the output of each computer to a text file.
# Name:
# Date: 02/10/2023
# Desc: Runs remote commands for every computer within domain to collect general information
#Defining variable that reads the computer names from the .txt file
$ADCS = Get-Content -Path "C:\computers.txt"
# Loop through each computer name in the list
foreach ($ADC in $ADCS) {
# Run the Invoke-Command cmdlet for each computer
Invoke-Command -ComputerName $ADC -ScriptBlock {
# Write message to indicate which computer commands are being run on.
Write-Output "Running command on $ADC"
#systeminfo | find "Host Name"
Get-computerinfo
out-file -FilePath C:\Users\aembrey\Documents\ComputerInfo.txt
}
} | out-file -FilePath C:\Users\aembrey\Documents\ComputerInfo.txt
This is what I am currently working with. I have tried multiple difference ways of using Out-file and have failed to redirect the output.
I tried formatting the command like you would as a standard command
"get-computerinfo | out-file -filepath C:\Users\X\X"
But I receive "An empty pipe element is not allowed." error.
I am sure this is a simple issue, but I am stumped.
Basically just trying to get the computer info of all computers, then save it to a text file.
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.
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."
I have a simple PowerShell script that copies a file from a mapped network drive, if it's modified in past 1 day.
$source = "Z:\\"
$target = "E:\target"
$files = get-childitem $source
foreach ($file in $files) {
if($file.LastWriteTime -ge (get-date).AddDays(-1)) {
Copy-Item $file.FullName $target
}
}
This script runs fine if I manually execute it.
If I try to use a scheduled task, the copy does not run. I confirmed the script is running by having it make a directory.
If I instead copy from a local drive instead of a network drive, the script runs fine with a scheduled task.
Schedule Task is running as an Admin Account.
Script copying file from network drive runs fine manually but not via scheduled task. Script runs fine as task if copying from local but not network drive.
Any ideas?
Try specifying the full UNC path rather than a network drive. (Network drives are a per-user configuration item.)
Map the drive as a temporary PowerShell drive...add the following as the first line of the script
New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\sharename
Current PS script:
Invoke-Command -ComputerName RemoteServer007.FQDN.com -ScriptBlock {
Set-Variable -Name WOWCONFIG -value "d:\ABCs\WOWzers" `
| Start-Process "d:\da-folder\Do-It-NOW-Pleez.cmd"
}
If I log on locally to the server(RemoteServer007.FQDN.com) and execute the cmd file, it runs through all of the lines(commands) within the cmd file.
When I execute it remotely, it gets about 30% of the way through the commands within the cmd file, the PS execution ends without error, but not all of the lines/commands in the cmd file had been executed.
This was discovered by simply configuring each line of the cmd file to output to txt files.
I even tried re-ranging the commands in the cmd file, thinking that perhaps there was a specific command that was causing it to exit, but that is not the case.
I'm wondering if there is some timeout or response that PowerShell is not getting? and just quitting almost immediately after starting?
Any ideas or help would be greatly appreciated.
There are a couple of things you can do here:
You may have a memory issue. Increasing the value of MaxMemoryPerShellMB might help
set-item WSMan:\$target\Shell\MaxMemoryPerShellMB -Value 0 -Force
You'd need to run this once on the remote machine before you execute your commands again.
You can also see possible error logs in the windows event viewer. There are categories for powershell and for Windows Remote Management which you should look at.
Finally, you can just run this process asynchronously, using the task scheduler for instance. I had a similar problem with windows in the past, and running the process from the task scheduler, outside the powershell session, fixed it. There's an example of how we did this in Cloudify here:
https://github.com/CloudifySource/cloudify/blob/master/esc/src/main/resources/clouds/ec2-win/upload/bootstrap-management.ps1#L220
I'm using the following powershell script to monitor new files coming
in an IBM iSeries shared folder.
# variables
#$folder = "\\10.10.0.120\transform\BE\FORM"
#$folder = "C:\Users\Administrator.ALI\Desktop\AS400"
#$folder = "\\nb091002\Temp"
$folder = "I:\"
$filter = "*.txt"
$aswform = "C:\ASWFORM\aswform.exe"
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $folder
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $false
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite -bor [System.IO.NotifyFilters]::FileName
while($TRUE){
$result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::Changed -bor [System.IO.WatcherChangeTypes]::Renamed -bOr [System.IO.WatcherChangeTypes]::Created, 2000);
if($result.TimedOut){
continue;
}
Write-Host $result.Name
#$aswform $folder
}
This seems to work fine on local folders or domain shares.
I've tried mapping the iSeries shared folder to a network drive but it doesn't work.
(10.10.0.120 is the AS400)
I'm pretty sure it has to do something with credentials....
Strange thing is I can access the shared folder from within Windows perfectly.
Does anybody have any clues or tips for me?
PS: little detail, I'll be running this script through task sheduler with this trigger
powershell -NoExit -WindowStyle Hidden -File "C:\ASWFORM\watcher.ps1"
But first I need it working when running the script manually!
I have not been able to get FileSystemWatcher to work unless the target directory was a Windows NTFS drive. If I specify the drive letter of the mapped directory I get
Exception setting "Path": "The directory name W:\ is invalid."
If I use the UNC I get
Exception calling "WaitForChanged" with "2" argument(s): "Error reading the \\\\path.to.my.ibm.i\\root\ directory."
Against a Novell file server I get the directory name is invalid if I use a drive letter. If I use a UNC against a Novell drive it does run, but doesn't detect any changes to the file system. Works fine against a local drive and also against a Windows file server on my network.
I solved the problem by writing a small C# console application to poll the folder
instead of using the .Net FileSystemWatcher object.
I manually (instsrv.exe) installed this program as a service and it seems to be running ok.
If you want the code, please send me a PM and I'll see to it that you get it.