Sunday, December 27, 2015

Retrieve log files from a specific date

Hi,

Below is a very basic script that simply retrieve log files from a specific date,
Based on the LastWriteTime attribute of the file.

In the script i'm mapping the destination machine drive, this is because i need to use different credentials to access the remote machine.

In case you have already permission to access the remote machine C$ share / other, adjust the script below, just to save time and issue using map drive.

Write-Host "Enter The Requested Date To Filter Files,`nUse The Following Example - 24/12:" -ForegroundColor Green
$RequestedDate = Read-Host
Write-Host "Enter Computer Name / IP, To Retreive Files From..." -ForegroundColor Green
$ComputerName = Read-Host

# Map Network Drive
net use O: \\$ComputerName\C$ /USER:BlaBla BlaBla

# Check If Local Temp Path Exists
if (!(Test-Path C:\Logs\$ComputerName)) {mkdir C:\Logs\$ComputerName}

$ClientLogs = Get-ChildItem O:\LocalApp\Client\Logs\ -Recurse
$ServerLogs =  Get-ChildItem O:\LocalApp\Server\Logs\


$ClientLogs_Results = $ClientLogs | Where-Object {$_.LastWriteTime.ToString("dd/MM") -eq $RequestedDate}
$ServerLogs_Results = $ServerLogs | Where-Object {$_.LastWriteTime.ToString("dd/MM") -eq $RequestedDate}

    foreach ($ServerLogs_R in $ServerLogs_Results) {

        Write-Host -ForegroundColor Green "Copying $ServerLogs_R"
        Copy-Item -LiteralPath $ServerLogs_R.fullname -Destination C:\Logs\$ComputerName -Force
        
    } # END of ServerLogs_Results Loop

    foreach ($ClientLogs_R in $ClientLogs_Results) {

        Write-Host -ForegroundColor Green "Copying $ClientLogs_R"
        Copy-Item -LiteralPath $ClientLogs_R.fullname -Destination C:\Logs\$ComputerName -Force
        
    } # END of ServerLogs_Results Loop

# Remove Map Network Drive
Net use O: /delete /y

Write-Host "`n`nPress Enter To Exit..." -ForegroundColor Green
Read-Host



Enjoy!

Tuesday, July 21, 2015

Retreive Monitor Serial Number

Retrieve Monitor Serial Number Using PowerShell

Small script to retrieve monitor serial number using PowerShell,
using a WMI method called WmiMonitorID under the root\wmi namespace.

This WMI method exist from Vista and above, if you are having XP and below, you still can retrieve the information using a registry query and convert the EDID to the monitor information which include the monitor serial number.

Script Below



# Import Computers from txt file
$Computers = Get-Content C:\temp\List.txt

# Foreach loop
$BigOutput = foreach ($Computer in $Computers) {
    
    # Create output object
    $output = "" | Select-Object -Property Computer, Manufacturer, ProductCode, SerialNumber, Name, Week, Year
    
    # Get all connected monitors using WMI class WmiMonitorID
    $Monitors = Get-WmiObject -Namespace root\wmi -Class WmiMonitorID -ComputerName $Computer

        # Internal foreach loop to go over each connected monitor
        foreach ($Monitor in $Monitors) {
            
            $output.Computer = $Computer
            # get monitor info and convert to char
            $Monitor.ManufacturerName | foreach {$output.Manufacturer += [char]$_}
            $Monitor.ProductCodeID | foreach {$output.ProductCode += [char]$_}
            $Monitor.SerialNumberID | foreach {$output.SerialNumber += [char]$_}
            $Monitor.UserFriendlyName | foreach {$output.Name += [char]$_}
            # Get week and year
            $output.Week = $Monitor.WeekOfManufacture
            $output.Year = $Monitor.YearOfManufacture
            $output
            $output = "" | Select-Object -Property Computer, Manufacturer, ProductCode, SerialNumber, Name, Week, Year

        } #END internal foreach loop

} # END foreach loop

# Export result to CSV file
$BigOutput | Export-Csv C:\GetMonitorSN.csv


Enjoy!



Tuesday, June 2, 2015

Load PowerCLI into PoweShell ISE

function Load-PowerCLI
{
    Add-PSSnapin VMware.VimAutomation.Core
    Add-PSSnapin VMware.VimAutomation.Vds
    . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"


Once execute you may connect to your vSphere using the following command:


Connect-VIServer -Server "Server"

 Replace the "Server" with your vSphere address / hostname,

You will receive a prompt for user name and password -



For some reason, since we are using the ISE interface we need to switch to the new PS-Drive created for us, in order to run PowerCLI commands.

cd vi:\

you may check your connection to the vSphere using a PowerCLI command, example -

get-vm
 
Enjoy!