Hi All,
It is my first post here but the people here I know from a long time from MS forums.
My issue that I have a txt files with servers name say for example: 2345-UHGFD and a friendly name MyServer-DC, so I want to get powershell to display both 2345-UHGFD and Myserver-DC when I run a report on these server.
My txt file now state:
2345-UHGFD
3456-UYTE
I need say to be bale to do:
2345-UHGFD ‘MyServer-DC’
3456-UYTE 'SQL-78KGP
Can this be done so it will show both from the txt file on the html report created by the powershell script?
Thanks
$servers = 1..100 | % {[pscustomobject]@{
'forex.'=((1..4 | % {65..90|get-random|%{[char]$_}}) -join '') + '-'+ ((1..4 | % {0..9|get-random|%{$_}}) -join '')
friendlyname = 'MyServer-DC'
}}
$servers |ConvertTo-Html | out-file createdbythepowershellscript.htm
The actual name is 2345-UHGFD and friendly name is MyServer-DC? How do you associate them? The computer name is 2345-UHGFD and descriptiogn is MyServer-DC? DNS alias? The answer is yes, you can process the real servername and list the alias. A manual CSV can be used, but it would be easier to just grab it from another source if you can provide details.
Thanks Dan for the code. But this will not help as I have a list of servers not one server. I have a txt file I pass to the powershell script that read this txt file. Due to the long number of these server I opted to name them in a friendly name. I run the script to check for disk space every day and also another script that ping these server to check which is up up and which down down and when server last seen or when it started!
a snippet here:
$freeSpaceFileName = “F:\disk_space.htm”
$serverlist = “F:\Script\Disksapce report\servers-list.txt”
$warning = 20
$critical = 10
New-Item $freeSpaceFileName -ItemType file -Force
Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File "C:\temp\FreeSpace.txt
Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString(‘dd.MM.yyyy - HH:mm’)
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName ‘Disk Report’
add-content $fileName ‘’
add-content $fileName “”
add-content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “Disk Report - $date”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
}
Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “Drive”
Add-Content $fileName “Drive Label”
Add-Content $fileName “Total Capacity(GB)”
Add-Content $fileName “Used Capacity(GB)”
Add-Content $fileName “Free Space(GB)”
Add-Content $fileName “Freespace %”
Add-Content $fileName “”
}
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “”
}
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace - $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
else
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
}
Function sendEmail
servers-list.txt is where the servers name
2345-UHGFD ‘MyServer-DC’
3456-UYTE 'SQL-78KGP
Can I add opposite each server the friendly name as above and how to make the script distinguish and add the friendly name to the right server name?
Thanks for everyone input and help.
Hi Rob and thanks for your response. Please see my response for Dan. Alias seem good idea but how I can implement it in txt filke that read by the powershell script?
Thanks
Hey Rambos, you said you have a text file with the server name and the “friendly name”. If this text file you referred to can ack as a cross reference, you can read it into your powershell script in and use it to find the friendly name to add to the output. Without seeing the a sample of the content of the text file; however, we cannot suggest what you need to do to read in the file and parse it correctly to provide the desired output.
My response was a near literal representation of what you asked for ;D
Try rephrasing the question and giving enough information. My servers only have one name.
Hi Curtis/Dan,
Okay let say I have over a 100 servers with names like:
servers-list.txt is where the servers name
2345-UHGFD
3456-UYTE
78998-HGFD09
…
I need to use the script to get the list of these servers from the servers-list.txt file, but I will need to add the friendly names opposite each server name so the report when it will run can be exported or converted to html/csv or any format I will be able to know what this server hosts when I look at the report and go direct to it instead of the unfriendly naming convention. I need to implement this solution in other scripts to run and check for other things on these servers.
So how can I implement this in my script and am I will be able to add the friendly name in servers-list.txt file?
Thanks
Rambos
To add the friendly names opposite each server name you can open notepad and add friendlyname after all of your servernames
just like this
`2345-UHGFD, MYServer-DC
3456-UYTE, MyServer-HC
78998-HGFD09, MyServer-ML`
and the right question… “How I can use this modified servers-list.txt in my script” can have answer close to
$servers = import-csv d:\servers-list.txt -delimiter ',' -Header unfriendlyname, friendlyname
foreach ($server in $servers) {
$info = Get-WmiObject win32_logicaldisk -ComputerName $server.unfriendlyname
foreach ($i in $info) {
'Free space on '+$server.friendlyname+' device '+$i.DeviceID+' - '+$i.FreeSpace
}
}