Skip to content

Instantly share code, notes, and snippets.

@elricstorm
Last active August 29, 2015 14:04
Show Gist options
  • Save elricstorm/175e30551bcc0d2a3860 to your computer and use it in GitHub Desktop.
Save elricstorm/175e30551bcc0d2a3860 to your computer and use it in GitHub Desktop.
Connect to remote computer
function con-remote {
# Look for a parameter to be sent for the computer name
param(
# Supply a computer name parameter
$name=$(Write-Host "You must specify the computer name as an argument."),
# Supply a routine to perform like OST
[string]$arg1
)
if ($name -ne $null) {
if (Test-Connection -Computername $name -BufferSize 16 -Count 1 -Quiet) {
Write-Host $name is online
Write-Host "Connecting to "$name"."
Set-Location \\$name\c$
if ($arg1 -ne $null) {
# Lets check OSTs
if ($arg1 -eq 'ost') {
$colFiles = Get-Wmiobject -namespace "root\CIMV2" `
-computername $name `
-Query "Select * from CIM_DataFile `
Where Extension = 'ost'"
foreach ($objFile in $colFiles) {
if($objFile.FileName -ne $null){
$filepath = $objFile.Drive + $objFile.Path + $objFile.FileName + "." `
+ $objFile.Extension;
$query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" `
+ $filepath `
+ "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
$colOwners = Get-Wmiobject -namespace "root\CIMV2" `
-computername $name `
-Query $query
$objOwner = $colOwners[0]
$user = $objOwner.ReferencedDomainName + "\" + $objOwner.AccountName
$filesize = $objFile.FileSize/1MB
Write-Host "===================================================================="
Write-Host ""
Write-Host "Computer:"$name -foregroundcolor "cyan"
Write-Host "Filepath:"$filepath -foregroundcolor "gray"
Write-Host "User:"$user -foregroundcolor "green"
Write-Host "Size:"$filesize" MB" -foregroundcolor "yellow"
Write-Host ""
Write-Host "===================================================================="
}
}
} else {
Write-Host "No valid routines requested."
}
}
} else {
Write-Host "Unable to connect to "$name"."
}
} else {
Write-Host "Re-run the command again as con-remote ComputerName."
}
}
# Connect to a computer
function con-remote {
# Look for a parameter to be sent for the computer name
param(
# Supply a computer name parameter
$name=$(Write-Host "You must specify the computer name as an argument."),
# Supply a routine to perform like OST
[string]$arg1
)
if ($name -ne $null) {
if (Test-Connection -Computername $name -BufferSize 16 -Count 1 -Quiet) {
Write-Host $name is online
Write-Host "Connecting to "$name"."
if ($arg1 -ne $null) {
# Lets check OSTs
if ($arg1 -eq 'ost') {
Write-Host "Searching for .OST files..."
$colFiles = Get-Wmiobject -namespace "root\CIMV2" `
-computername $name `
-Query "Select * from CIM_DataFile `
Where Extension = 'ost'"
foreach ($objFile in $colFiles) {
if($objFile.FileName -ne $null){
$filepath = $objFile.Drive + $objFile.Path + $objFile.FileName + "." `
+ $objFile.Extension;
$query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" `
+ $filepath `
+ "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
$colOwners = Get-Wmiobject -namespace "root\CIMV2" `
-computername $name `
-Query $query
$objOwner = $colOwners[0]
$user = $objOwner.ReferencedDomainName + "\" + $objOwner.AccountName
$filesize = $objFile.FileSize/1MB
$ostpath = $objFile.Path;
$ostfile = $objFile.FileName + "." + $objFile.Extension;
Write-Host "===================================================================="
Write-Host ""
Write-Host "Computer:"$name -foregroundcolor "cyan"
Write-Host "Filepath:"$filepath -foregroundcolor "gray"
Write-Host "User:"$user -foregroundcolor "green"
Write-Host "Size:"$filesize" MB" -foregroundcolor "yellow"
Write-Host ""
Write-Host "===================================================================="
Write-Host ""
Set-Location \\$name\c$\$ostpath
Write-Host "Deleting OST File..."
Remove-Item \\$name\c$\$ostpath\$ostfile
Get-ChildItem
}
}
} else {
Write-Host "No valid routines requested."
}
}
# Change to the root of C$
Set-Location \\$name\c$
} else {
Write-Host "Unable to connect to "$name"."
}
} else {
Write-Host "Re-run the command again as con-remote ComputerName."
}
}
# Connect to a computer
function con-remote {
# Look for a parameter to be sent for the computer name
param($name=$(Write-Host "You must specify the computer name as an argument."))
if ($name -ne $null) {
if (Test-Connection -Computername $name -BufferSize 16 -Count 1 -Quiet) {
Write-Host $name is online
Write-Host "Connecting to "$name"."
Set-Location \\$name\c$
} else {
Write-Host "Unable to connect to "$name"."
}
} else {
Write-Host "Re-run the command again as con-remote ComputerName."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment