Monday, August 30, 2010

Format a volume larger than 32 GB in FAT32

Windows XP doesn't permit you to format FAT32 drive more than 32 gb
(see KB Q314463 Limitations of the FAT32 File System in Windows XP)

You can do this by command line using /FS switch but that doesn't work everytime

format /FS:FAT32 yourletter:




So you can use some tools

Fat32Format
http://www.ridgecrop.demon.co.uk/index.htm?fat32format.htm or
version with GUI (Alternate download location) or commandline (Alternate download location)

or

Fat32Formatter
http://tokiwa.qee.jp/EN/Fat32Formatter/ (Alternate download location)

Friday, August 13, 2010

Powershell get encoding file type

Like I need to know file encoding type programmatically, I first search and found on internet a script which permit to do it. I just modified it to add some encoding type

On this page http://poshcode.org/2059 original code I also copied below (Sorry, I don't know original author for greetings)


<# .SYNOPSIS Gets file encoding. .DESCRIPTION The Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM). Based on port of C# code from http://www.west-wind.com/Weblog/posts/197245.aspx .EXAMPLE Get-ChildItem *.ps1 select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} where {$_.Encoding -ne 'ASCII'} This command gets ps1 files in current directory where encoding is not ASCII .EXAMPLE Get-ChildItem *.ps1 select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} where {$_.Encoding -ne 'ASCII'} foreach {(get-content $_.FullName) set-content $_.FullName -Encoding ASCII} Same as previous example but fixes encoding using set-content #>
function Get-FileEncoding
{
[CmdletBinding()] Param (
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] [string]$Path
)

[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path

if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
{ Write-Output 'UTF8' }
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
{ Write-Output 'Unicode' }
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
{ Write-Output 'UTF32' }
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76)
{ Write-Output 'UTF7'}
else
{ Write-Output 'ASCII' }
}


Below my little changes to detect more encoding format refering http://en.wikipedia.org/wiki/Byte_order_mark



<#
.SYNOPSIS
Gets file encoding.
.DESCRIPTION
The Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM).
Based on port of C# code from http://www.west-wind.com/Weblog/posts/197245.aspx
.EXAMPLE
Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'}
This command gets ps1 files in current directory where encoding is not ASCII
.EXAMPLE
Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII}
Same as previous example but fixes encoding using set-content


# Modified by F.RICHARD August 2010
# add comment + more BOM
# http://unicode.org/faq/utf_bom.html
# http://en.wikipedia.org/wiki/Byte_order_mark
#
# Do this next line before or add function in Profile.ps1
# Import-Module .\Get-FileEncoding.ps1
#>
function Get-FileEncoding
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
[string]$Path
)

[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
#Write-Host Bytes: $byte[0] $byte[1] $byte[2] $byte[3]

# EF BB BF (UTF8)
if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
{ Write-Output 'UTF8' }

# FE FF (UTF-16 Big-Endian)
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
{ Write-Output 'Unicode UTF-16 Big-Endian' }

# FF FE (UTF-16 Little-Endian)
elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe)
{ Write-Output 'Unicode UTF-16 Little-Endian' }

# 00 00 FE FF (UTF32 Big-Endian)
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
{ Write-Output 'UTF32 Big-Endian' }

# FE FF 00 00 (UTF32 Little-Endian)
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff -and $byte[2] -eq 0 -and $byte[3] -eq 0)
{ Write-Output 'UTF32 Little-Endian' }

# 2B 2F 76 (38 | 38 | 2B | 2F)
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76 -and ($byte[3] -eq 0x38 -or $byte[3] -eq 0x39 -or $byte[3] -eq 0x2b -or $byte[3] -eq 0x2f) )
{ Write-Output 'UTF7'}

# F7 64 4C (UTF-1)
elseif ( $byte[0] -eq 0xf7 -and $byte[1] -eq 0x64 -and $byte[2] -eq 0x4c )
{ Write-Output 'UTF-1' }

# DD 73 66 73 (UTF-EBCDIC)
elseif ($byte[0] -eq 0xdd -and $byte[1] -eq 0x73 -and $byte[2] -eq 0x66 -and $byte[3] -eq 0x73)
{ Write-Output 'UTF-EBCDIC' }

# 0E FE FF (SCSU)
elseif ( $byte[0] -eq 0x0e -and $byte[1] -eq 0xfe -and $byte[2] -eq 0xff )
{ Write-Output 'SCSU' }

# FB EE 28 (BOCU-1)
elseif ( $byte[0] -eq 0xfb -and $byte[1] -eq 0xee -and $byte[2] -eq 0x28 )
{ Write-Output 'BOCU-1' }

# 84 31 95 33 (GB-18030)
elseif ($byte[0] -eq 0x84 -and $byte[1] -eq 0x31 -and $byte[2] -eq 0x95 -and $byte[3] -eq 0x33)
{ Write-Output 'GB-18030' }

else
{ Write-Output 'ASCII' }
}


To use this function paste it to your profile.ps1 file or copy file on a folder and use this next line before use the function

use import-module .\Get-FileEncoding.ps1

Then use it by something like

Get-Item *.log | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}}


You can download this file here

Wednesday, August 4, 2010

Get alias cname hostname DNS using powershell

Well, just a way in powershell to know real hostname alias (CNAME)


$computername = [System.Net.Dns]::GetHostByName("youralias")
Write-Host $computername.HostName.substring(0,$computername.HostName.IndexOf("."))

Friday, July 30, 2010

Powershell to display Storage Model for each VMWare VM

Script below permit to display which storage model is used for each VM of you Virtual Center
Download it


#
# Storage Model by VM
#
# by Franck RICHARD
# 2010 July
#

# Connect VC
$VC = "localhost"
Connect-VIServer -Server $VC | Out-Null

# Get all Datastores
$datastores = Get-Datastore
# Get All VMs
$ArrVM = Get-VM
foreach ($VM in $arrVM) {
# Get VMHost View
$VMHostView = Get-VMHost -VM $VM | Get-View

# Get all HardDisks
$HardDisks = $VM | Get-HardDisk
foreach ($HardDisk in $HardDisks) {
# Filename = "[VMFS-PROD] COMPUTER01/COMPUTER01.vmdk" for example
$DatastoreVM = $HardDisk.Filename.Split("[]")[1]
$DatastoreVMView = $datastores | where {$_.name -eq $DatastoreVM} | Get-View

# Diskname = "vmhba2:0:20" for example
$VMFSextents = $DatastoreVMView.info.vmfs.extent
foreach ($VMFSextent in $VMFSextents) {
$Diskname = $vmfsextent.Diskname
#Write-Host "Diskname:"$Diskname
}

$ScsiLuns = $VMHostView.Config.StorageDevice.ScsiLun
$bDiskFound = $False
foreach ($ScsiLun in $ScsiLuns) {
if ($ScsiLun.CanonicalName -eq $Diskname) {
#For Example
# CanonicalName = "vmhba2:0:0"
# Vendor = "EMC"
# Model = "SYMMETRIX"
$bDiskFound = $True
break
}
}

if ($bDiskFound) {
Write-Host "Name:" $VM.Name $HardDisk.Name $ScsiLun.CanonicalName "Vendor:"$ScsiLun.Vendor "Model:"$ScsiLun.Model
} else {
Write-Host "Name:" $VM.Name $diskname "not found"
}
# break # if all HardDisks are on the SAN
} # Foreach $HardDisk
} # Foreach $VM

Saturday, July 10, 2010

Site WWoIT - Wayne's World of IT

Site WWoIT - Wayne's World of IT
http://waynes-world-it.blogspot.com/
Some very interesting information about vmware and windows infrastructure

Tuesday, June 29, 2010

Filter all lines of a text file with multiple strings list

To display only lines containing a string in a text file, easiest way is to use findstr commandline. For example, if we want to display lines containing "ok" (with /i switch for no case sensitivity) in a file file.txt we just to run at command line something like

type file.txt | findstr /i ok


With findstr you can also display lines NOT containing a particular string. For example you want all lines except line with string "not good" with this command

type file.txt | findstr /i /v /c:"not good"


Today my problem is that I need to report all administrators users of all computers except some users in a list.(yes, there is not SCCM or another tool which permit to do it here). I could do it like this:

type file.txt findstr /i /v /c:"administrator1" findstr /i /v /c:"administrator2" findstr /i /v /c:"administrator3"


But that's not really simple to maintain if you need to do some changes. Moreover, why do not do something which could be used for others problems of the same type?
So to do better than excel :) I created this batch file filter.cmd

@echo off
REM
REM Line Filter
REM By F.RICHARD
REM
REM created 2010 July
REM modified 2010 september for %1
REM

SET CURDIR=%~d0%~p0
cd /d %CURDIR%

IF {%1}=={} GOTO :ERRFILEINPUT
SET INPUTFILE=%1
echo Input file:%INPUTFILE%
If NOT EXIST %INPUTFILE% GOTO :ERRNEINPFILE
IF {%2}=={} GOTO :ERRFILEFILTER
SET FILTERFILE=%2
echo Filter file:%FILTERFILE%
If NOT EXIST %FILTERFILE% GOTO :ERRNEFILFILE
IF {%3}=={} GOTO :ERRFILERESULT
SET RESULTFILE=%3
echo Result file:%RESULTFILE%

setlocal EnableDelayedExpansion
set /a nbstr=0

Echo Begin filter
type %INPUTFILE% > tempfile!nbstr!.txt
for /F "tokens=*" %%i IN (%FILTERFILE%) Do (
set tmpfileread=tempfile!nbstr!.txt
set /a nbstr=!nbstr!+1
set tmpfileresult=tempfile!nbstr!.txt
type !tmpfileread! | findstr /v /i /c:%%i > !tmpfileresult!
del /f /q !tmpfileread!
)
move /Y !tmpfileresult! %RESULTFILE%
Echo End filter
goto :EOF

:ERRFILEINPUT
Echo.
Echo ERROR: you need an input filename
goto :Syntax

:ERRFILEFILTER
Echo.
Echo ERROR: you need a filter file
goto :Syntax

:ERRFILERESULT
Echo.
Echo ERROR: you need a result filename
goto :Syntax

:ERRNEINPFILE
Echo.
Echo ERROR: input file - %INPUTFILE% - does not exist in %CURDIR% directory
goto :EOF

:ERRNEFILFILE
Echo.
Echo ERROR: filter file - %FILTERFILE% - does not exist in %CURDIR% directory
goto :EOF

:Syntax
Echo.
Echo Syntax:
Echo %~nx0 inputfile filterfile resultfile
Echo.
goto :EOF


use it with something like

stringfilter.cmd input.txt filter.txt result.txt


With a input file like this input.txt

aaaaaaa
do not want to see it
bbbbbbb
not this one
ccccccc


This batch use a filter file like this filter.txt

"not this"
bbbbbbb
"not want"


and result file was like this result.txt

aaaaaaa
ccccccc


Et voilà

You can download this script
here

Saturday, June 26, 2010

Get all OU's computers in a AD

If you need to have all computers of a specific OU, just run




@dsquery computer "OU=YourOU,DC=Domain,DC=Net" -limit 0 -o rdn > Computers_YourOu_domain_net.txt


Result will be savec in a file Computers_YourOu_domain_net.txt that will be like this:

"computer1"
"computer2"
"computer3"


Now you can use it to run a script on each OU's computers :-)