Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Friday, January 05, 2007

Here's some powershell script/command to find who is logged in on a RemoteMachine.

Get-WmiObject -computerName ComputerName -class Win32_ComputerSystem -Property UserName | findstr UserName

Wednesday, January 03, 2007

Finding dependencies for .net assemblies

Have you had .net assembly version mismatch problems?
How do you find which assembly is referencing an older version of some shared assembly?

Here is a powershell script to list out all the dependencies for all the files in given folder (typically bin/debug or bin/release)
Get script here

what it does is really easy and i must give credit to this guy

Friday, December 22, 2006

Powershell Network Scan - Script

Here's the code/script for network scan I was talking about in previous post here


function HandleShare
{
$share = $args[0];
write-host ***Scanning Share $share
#write-host net use X: $share
$erroractionpreference = "SilentlyContinue";
$files = Get-ChildItem -recurse -name $share $fileType
$erroractionpreference = "Continue";

foreach($file in $files)
{
if($file.length -gt 0)
{
$filepath = $share + "\" + $file;
write-host $filepath
}
}

}

function HandleHost
{
$hostname = $args[0];
$hostname = $hostname.substring(0, $hostname.indexof(" "))
#write-host "***Scanning Host " + $hostname;

#List shares.
$shares = net view $hostname | findstr " Disk"
foreach($share in $shares)
{
if($share.length -gt 0)
{
$tmp = $share.substring(0, $share.indexof("Disk"));
$tmp = $tmp.Trim();
$tmp = $hostname + "\" + $tmp;
HandleShare $tmp;
}
}
}

function DisplayUsage
{
write-host
write-host -foregroundcolor green "Usage:"
write-host -foregroundcolor green " scan.ps1 fileType maxNoOfMachines"
write-host -foregroundcolor green " e.g. scan.ps1 *.txt 10"
write-host
}

function Main
{
$hostnames = net view | findstr "\\";
foreach($hostname in $hostnames)
{
$maxNoOfMachines = $maxNoOfMachines - 1;
if($maxNoOfMachines -lt 0)
{
exit;
}

HandleHost $hostname;
}
}

#
# Initialise
#

#Guard against un initialised variables. aka, typos
set-psdebug –strict

#using $fluff as i dint like LoadWithPartialName o/p somethign to console.
$fluff = [reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

#
# Validate input paramters
#
if($args.count -lt 2 -or $args.count -gt 2)
{
DisplayUsage;
exit;
}
if(-not([Microsoft.VisualBasic.Information]::isnumeric($args[1])))
{
DisplayUsage;
write-host -foregroundcolor Red "Error:"
write-host -foregroundcolor Red " Second paramter should be numeric."
write-host
exit;
}

#
# Convert args into variables which are used through out this script.
#

$maxNoOfMachines = $args[1];
$fileType = $args[0]; #Global variable used in HandleShare.
#$maxNoOfMachines = 30; #Global variable used in Main.

#
# Start actual work
#

Main;