Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, August 9, 2014 8:59 PM
Hi
I am very new to scripting world. I have written a basic code which does the job of deleting temp files under the listed folder path on a remote server. But as I see this process is taking quite a long time to process. I am hoping to find an other way to fasten this. Is it possible to push a script to the remote system first and run that locally? If I am not wrong that would make it process faster.
I would also ask you to help me formatting this raw code. I want to introduce a logging system to this. Also make it work for multiple servers by fetching the list of servers from a csv. Thanks a lot in advance!!!!!
Code:
param (
[string]$ComputerName = $(throw "ComputerName(s) is required.")
)
Clear-Host
foreach ($computer in $ComputerName.Split(" "))
{
if (Test-Connection -ComputerName $Computer -ErrorAction SilentlyContinue)
{
Write-host ("About to clean : "+$computer)
$disk = ([wmi]"\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")
"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - Before Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output
Write-output "Cleaning is in progress, Please wait"
Remove-Item -Recurse -Force \$ComputerName\c$\temp\
Remove-Item -Recurse -Force \$ComputerName\c$\$Recycle.bin'\
Remove-Item -Recurse -Force -erroraction 'silentlycontinue' \$ComputerName\c$\Windows\Temp\
$disk = ([wmi]"\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")
"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - After Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output
}
}
All replies (1)
Saturday, August 9, 2014 9:25 PM âś…Answered
Check out the Invoke-Command to remotely run code
http://technet.microsoft.com/en-us/library/hh849719.aspx
To write a recursive function that steps through a folder structure with logging check out
batch file for checking every folder
http://social.technet.microsoft.com/Forums/en-US/9e38a05e-1306-4fea-9d5b-3e256fca52a9/batch-file-for-checking-every-folder?forum=ITCG
Your code properly formatted without regard to syntax or correctness.
param ([string]$ComputerName = $(throw "ComputerName(s) is required."))
Clear-Host
foreach ($computer in $ComputerName.Split(" "))
{
if (Test-Connection -ComputerName $Computer -ErrorAction SilentlyContinue)
{
Write-host ("About to clean : "+$computer)
$disk = ([wmi]"\\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")
"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - Before Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output
Write-output "Cleaning is in progress, Please wait"
Remove-Item -Recurse -Force \\$ComputerName\c$\temp\*
Remove-Item -Recurse -Force \\$ComputerName\c$\'$Recycle.bin'\*
Remove-Item -Recurse -Force -erroraction 'silentlycontinue' \\$ComputerName\c$\Windows\Temp\*
$disk = ([wmi]"\\$Computer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")
"$Computer C: has {0:#.00} GB free of {1:#.00} GB Total - After Cleaning" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output
}
}
To import a CSV file, we'll assume a csv file (C:\Computers.csv) like this
ComputerName,IPAddress
Computer1, 192.168.0.1
Computer2, 192.168.0.2
To step through the csv file and call a function with the contents as parameters
$csv = Import-CSV -Path "C:\Computers.csv"
ForEach($Line in $csv){
$ComputerName=$Line.ComputerName
$IPAddress=$Line.IPAddress
Test-Function -ComputerName $ComputerName -IPAddress $IPAddress
}
Hope this helps