how to extent this code to accommodate D and E drives as well - extent

I would like to extent the following code to accommodate all other drives on my machine.
Below code was written only on C drive..i am having difficulty modifying it, tried seperating values for driveletters, but the code is not displaying anything when run..
$ServerListFile = "D:\serverList.txt"
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue
$Result = #()
ForEach($c`enter code here`omputername in $ServerList)
{
$AVGProc = Get-WmiObject -computername $computername win32_processor |
Measure-Object -property LoadPercentage -Average | Select Average
$OS = gwmi -Class win32_operatingsystem -computername $computername |
Select-Object #{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize -
$_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
$vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" |
Select-object #{Name = "C PercentFree"; Expression = {“{0:N2}” -f (($_.FreeSpace / $_.Capacity)*100)
} }
$result += [PSCustomObject] #{
ServerName = "$computername"
CPULoad = "$($AVGProc.Average)%"
MemLoad = "$($OS.MemoryUsage)%"
CDrive = "$($vol.'C PercentFree')%"
}
$Outputreport = "<HTML><TITLE> Server Health Report </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> Server Health Report </H2></font>
<Table border=1 cellpadding=0 cellspacing=0>
<TR bgcolor=gray align=center>
<TD><B>Server Name</B></TD>
<TD><B>Avrg.CPU Utilization</B></TD>
<TD><B>Memory Utilization</B></TD>
<TD><B>Drive C Free Space</B></TD>
</TR>"
Foreach($Entry in $Result)
{
if(($Entry.CpuLoad) -or ($Entry.memload) -ge "80")
{
$Outputreport += "<TR bgcolor=white>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD
align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.CDrive)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file "D:\Result $(Get-Date -Format yyy-mm-dd-hhmm).htm"

Related

Rangeslider ACF in Foreach loop

I have a range slider in my Advanced custom fields. I want the number to be the amount of list items that it echo's out. In example, i put the range slider on 4 in ACF then it needs to echo out:
Flywheel 1
Flywheel 2
Flywheel 3
Flywheel 4
This is the code i have but it's not working, can someone please fix the code?
<?php
$flywheelnumber = get_field('flywheels');
if( $flywheelnumber ) {
echo '<form class="checklist-list">';
echo '<ul>';
foreach($flywheelnumber) {
echo '<li>';
echo ( $numbers );
echo '</li>';
}
echo '</ul>';
echo '</form>'
}
?>
Thanks!!
you have to use for loop. you can try these codes,
<?php
$flywheelnumber = get_field( 'flywheels' );
if ( $flywheelnumber ) {
echo '<form class="checklist-list">';
echo '<ul>';
for ( $x = 1; $x <= count( $flywheelnumber ); $x ++ ) {
echo '<li>' . $x . '</li>';
}
echo '</ul>';
echo '</form>';
}
?>

Parse wikipedia {{Location map}} templates

I would like to parse the Wikipedia power plant lists, which contain the {{Location map}} template. In my example I'm using the German translation, but this shouldn't change the basic process.
How can I get out the label=, lat=, lon= and region= parameters from such code?
Probably this is nothing for a html parser like BeautifulSoup, but rather awk?
{{ Positionskarte+
| Tadschikistan
| maptype = relief
| width = 600
| float = right
| caption =
| places =
{{ Positionskarte~
| Tadschikistan
| label = <small>[[Talsperre Baipasa|Baipasa]]</small>
| marktarget =
| mark = Blue pog.svg
| position = right
| lat = 38.267584
| long = 69.123906
| region = TJ
| background = #FEFEE9
}}
{{ Positionskarte~
| Tadschikistan
| label = <small>[[Kraftwerk Duschanbe|Duschanbe]]</small>
| marktarget =
| mark = Red pog.svg
| position = left
| lat = 38.5565
| long = 68.776
| region = TJ
| background = #FEFEE9
}}
...
}}
Thanks in advance!
Just extract information with regular expressions.
For example like this (PHP)
$k = "{{ Positionskarte+
| Tadschikistan
| maptype = relief
| width = 600
| float = right
| caption =
| places =
{{ Positionskarte~
| Tadschikistan
| label = <small>[[Talsperre Baipasa|Baipasa]]</small>
| marktarget =
| mark = Blue pog.svg
| position = right
| lat = 38.267584
| long = 69.123906
| region = TJ
| background = #FEFEE9
}}
{{ Positionskarte~
| Tadschikistan
| label = <small>[[Kraftwerk Duschanbe|Duschanbe]]</small>
| marktarget =
| mark = Red pog.svg
| position = left
| lat = 38.5565
| long = 68.776
| region = TJ
| background = #FEFEE9
}}
}}";
$items = explode("Positionskarte~", $k);
$result = [];
foreach ($items as $item) {
$info = [];
$pattern1 = '/label\s+=\s+(.+)/';
preg_match($pattern1, $item, $matches);
if (!empty($matches)) {
$info['label'] = $matches[1];
}
$pattern2 = '/lat\s+=\s+(.+)/';
preg_match($pattern2, $item, $matches);
if (!empty($matches)) {
$info['lat'] = $matches[1];
}
$pattern3 = '/long\s+=\s+(.+)/';
preg_match($pattern3, $item, $matches);
if (!empty($matches)) {
$info['long'] = $matches[1];
}
$pattern4 = '/region\s+=\s+(.+)/';
preg_match($pattern4, $item, $matches);
if (!empty($matches)) {
$info['region'] = $matches[1];
}
if(!empty($info)) {
$result[] = $info;
}
}
var_dump($result);

Parsing robocopy log file to PSCustomObject

I'm trying to create a PSCustomObject from a robocopy log file. The first piece is pretty easy but I'm struggling with the $Footer part. I can't seem to find a good way to split up the values.
It would be nice if every entry has it's own Property, so it's possible to use for example $Total.Dirs or $Skipped.Dirs. I was thinking about Import-CSV, because that's just great on how it allows you to have column headers. But this doesn't seem to fit here. There's another solution I found here but it seems a bit of overkill.
Code:
Function ConvertFrom-RobocopyLog {
Param (
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[String]$LogFile
)
Process {
$Header = Get-Content $LogFile | select -First 10
$Footer = Get-Content $LogFile | select -Last 7
$Header | ForEach-Object {
if ($_ -like "*Source*") {$Source = (($_.Split(':'))[1]).trim()}
if ($_ -like "*Dest*") {$Destination = (($_.Split(':'))[1]).trim()}
}
$Footer | ForEach-Object {
if ($_ -like "*Dirs*") {$Dirs = (($_.Split(':'))[1]).trim()}
if ($_ -like "*Files*") {$Files = (($_.Split(':'))[1]).trim()}
if ($_ -like "*Times*") {$Times = (($_.Split(':'))[1]).trim()}
}
$Obj = [PSCustomObject]#{
'Source' = $Source
'Destination' = $Destination
'Dirs' = $Dirs
'Files' = $Files
'Times' = $Times
}
Write-Output $Obj
}
}
Log file:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Wed Apr 01 14:28:11 2015
Source : \\SHARE\Source\
Dest : \\SHARE\Target\
Files : *.*
Options : *.* /S /E /COPY:DAT /PURGE /MIR /Z /NP /R:3 /W:3
------------------------------------------------------------------------------
0 Files...
0 More Folders and files...
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 2 0 2 0 0 0
Files : 203 0 203 0 0 0
Bytes : 0 0 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : Wed Apr 01 14:28:12 2015
Thank you for your help.
You can clean this up more but this is the basic approach I would take.
Function ConvertFrom-RobocopyLog {
Param (
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[String]$LogFile
)
Process {
$Header = Get-Content $LogFile | select -First 10
$Footer = Get-Content $LogFile | select -Last 7
$Header | ForEach-Object {
if ($_ -like "*Source*") {$Source = (($_.Split(':'))[1]).trim()}
if ($_ -like "*Dest*") {$Destination = (($_.Split(':'))[1]).trim()}
}
$Footer | ForEach-Object {
if ($_ -like "*Dirs*"){
$lineAsArray = (($_.Split(':')[1]).trim()) -split '\s+'
$Dirs = [pscustomobject][ordered]#{
Total = $lineAsArray[0]
Copied = $lineAsArray[1]
Skipped = $lineAsArray[2]
Mismatch = $lineAsArray[3]
FAILED = $lineAsArray[4]
Extras = $lineAsArray[5]
}
}
if ($_ -like "*Files*"){
$lineAsArray = ($_.Split(':')[1]).trim() -split '\s+'
$Files = [pscustomobject][ordered]#{
Total = $lineAsArray[0]
Copied = $lineAsArray[1]
Skipped = $lineAsArray[2]
Mismatch = $lineAsArray[3]
FAILED = $lineAsArray[4]
Extras = $lineAsArray[5]
}
}
if ($_ -like "*Times*"){
$lineAsArray = ($_.Split(':',2)[1]).trim() -split '\s+'
$Times = [pscustomobject][ordered]#{
Total = $lineAsArray[0]
Copied = $lineAsArray[1]
FAILED = $lineAsArray[2]
Extras = $lineAsArray[3]
}
}
}
$Obj = [PSCustomObject]#{
'Source' = $Source
'Destination' = $Destination
'Dirs' = $Dirs
'Files' = $Files
'Times' = $Times
}
Write-Output $Obj
}
}
I wanted to make a function to parse the footer lines but $Times is a special case since it does not have all the same columns of data.
With $Times the important difference is how we are doing the split. Since the string contains more than one colon we need to account for that. Using the other paramenter in .Split() we specify the number of elements to return.
$_.Split(':',2)[1]
Since these logs always have output and no blanks row elements we can do this assuming that the parsed elements of $lineAsArray will always have 6 elements.
Sample Output
Source : \\SHARE\Source\
Destination : \\SHARE\Target\
Dirs : #{Total=2; Copied=0; Skipped=2; Mismatch=0; FAILED=0; Extras=0}
Files : #{Total=203; Copied=0; Skipped=203; Mismatch=0; FAILED=0; Extras=0}
Times : #{Total=0:00:00; Copied=0:00:00; FAILED=0:00:00; Extras=0:00:00}
So if you wanted the total files copied you can now use dot notation.
(ConvertFrom-RobocopyLog C:\temp\log.log).Files.Total
203
Not that clear what you want to do, but this will go some way to showing you how to get the stats into an array of objects
$statsOut = #()
$stats = Get-Content $LogFile | select -Last 6 | select -first 4
$stats | % {
$s = $_ -split "\s+"
$o = new-object -type pscustomobject -property #{"Name"=$s[0];"Total"=$s[2];"Copied"=$s[3];"Skipped"=$s[4];"mismatch"=$s[5]};
$statsOut += ,$o
}
Gives:
[PS] > $statsOut | ft -Auto
mismatch Name Skipped Total Copied
-------- ---- ------- ----- ------
0 Dirs 2 2 0
0 Files 203 203 0
0 Bytes 0 0 0

Extract data and log file path in sql server using powershell

I'm trying to get data and log file information using the below script and arrange the output as per the below xml node..
param
(
$ClientName,
$Sourcedatabase,
$DestServer,
$DESTINATIONDB
)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$DataSet = New-Object System.Data.DataSet
$SqlConnection.ConnectionString = "Server = $DestServer; Database = '$DESTINATIONDB'; Integrated Security = True"
$SqlCmd.CommandText = "select f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and
d.name = 'master'"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($DataSet)|out-null
foreach ($Row in $Dataset.Tables[0].Rows)
{
$dev1 = $($Row[0])
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$DataSet = New-Object System.Data.DataSet
$SqlConnection.ConnectionString = "Server = $ClientName; Database = $Sourcedatabase; Integrated Security = True"
$SqlCmd.CommandText = "select f.name ,f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and
d.name = '$Sourcedatabase'"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($DataSet)|out-null
foreach ($Row in $Dataset.Tables[0].Rows)
{
if($dev1.EndsWith(".mdf") -and $($Row[1]).EndsWith(".mdf"))
{
$dev = "<device>|" + $Sourcedatabase + "|#12!" + $DESTINATIONDB
$dev = $dev + "|#12!" + $($Row[0]) +"|#12!"
$dev = $dev + $dev1 +"|#12!"
$dev = $dev + $($Row[1])
$dev = $dev + "</device>"
$dev
}
if($dev1.EndsWith(".ldf") -and $($Row[1]).EndsWith(".ldf"))
{
$dev = "<device>|" + $Sourcedatabase + "|#12!" + $DESTINATIONDB
$dev = $dev + "|#12!" + $($Row[0]) +"|#12!"
$dev = $dev + $dev1 +"|#12!"
$dev = $dev + $($Row[1])
$dev = $dev + "</device>"
$dev
}
}
}
the output should be like
|test_restore|#12!testCDC|#12!test_replication|#12!D:\MSSQL\DATA\MSSQL10.MSSQLSERVER\MSSQL\DATA\master.mdf|#12!D:\MSSQL\DATA\Test
_Restore.mdf
|test_restore|#12!testCDC|#12!test_replication_log|#12!D:\MSSQL\DATA\MSSQL10.MSSQLSERVER\MSSQL\DATA\mastlog.ldf|#12!L:\MSSQL\LOG\
Test_Restore_log.ldf
I'm able to get the output...but if a database has more than one data file...then i get only one data file...
please help me on this...
thanks in advance...
I think You are doing this the hard way... you should try to use powershell properly
Step 1: If you are using sqlserver 2008
Ensure you have sql snappins registered and added:
https://blogs.msdn.com/b/dtjones/archive/2011/03/31/loading-sql-server-snapins-into-your-powershell-session.aspx?Redirected=true
you should be able to do this in you ps cmdline (and it not fail)
set-Location Sqlserver:\
or
Get-PSProvider should return a line with sqlserver in it
this means the you can interrogate sqlserver in a rest/directory style
step 2:
cd SQLSERVER:\SQL\YOURSQLSERVERNAME\default\databases\YOURDATABASENAME\logfiles
replacing YOURDATABASENAME and YOURSQLSERVERNAME (usually your computerName)
now you can run dir this this "directory"
or
dir | select FileName
(dir).FileName

Foreach command with a folder with many files using a template txt file

Using the following code to select help select 1 file from a list of many dated files and then copy to share. Using the following code very kindly provided by fellow Stacker
$pastdays = -7
$pastdate = [datetime]::Now.AddDays($pastdays)
$files = Get-ChildItem "V:\Capacity Manager Size reports\"
$filename = ($files | Where-Object {$_.Name -like "Database_Server1_Userdb1_" + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})
Copy-Item -Path $filename.FullName "\\Server50\report_Archive"
I need to expand this process to run for many files in the folder – each with slightly different names but following a common template to the file name
Original filenames examples:
Database_Server1_UserDB1_30_10_2012_00_20_51
Database_Server2_UserDB2_30_10_2012_01_20_51
Etc
Etc
But im not sure how to best do this in a foreach ?
I was thinking create a template file to load into a variable that holds all the examples of the files to examined before copying
So:
$Report_Template = Get-Content "C:\Powershell\FileTemplate_Name.txt";
FileTemplate_name.txt:
Database_Server1_tempdb_
Database_Server1_UserDB1_
Database_Server1_UserDB2_
Database_Server2_UserDB2_
Database_Server2_UserDB4_
$Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";
$pastdays = -7
$pastdate = [datetime]::Now.AddDays($pastdays)
$files = Get-ChildItem "V:\Capacity Manager Size reports\"
foreach($template in $Report_Template)
$filename = ($files | Where-Object {$_.Name -like $template + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})
Copy-Item -Path $filename.FullName "\\Server50\CapacityManagerReports_Archive\"
Try this:
$Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";
$pastdays = -7
$pastdate = [datetime]::Now.AddDays($pastdays)
$files = Get-ChildItem "V:\Capacity Manager Size reports\"
foreach($template in $Report_Template)
{
$filename = ($files | Where-Object {$_.Name -like $template + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})
Copy-Item -Path $filename.FullName "\\Server50\CapacityManagerReports_Archive\"
}
But do you really need template file? Can't you use file extension, or some part of the name, or just copy everything from this date? Maybe your inital folder with files has wrong structure and you should better switch to multiple folders, like separate folder for each Database_Server1?
Ive done the following which seems to work
$Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";
$pastdays = -3
$pastdate = [datetime]::Now.AddDays($pastdays)
$Results = Get-ChildItem "V:\Capacity Manager Size reports\"
Foreach ($Files in $Results)
{foreach($template in $Report_Template)
{If ($files.Name -like $($template + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year +"*.pdf"))
{Copy-Item -Path $files.FullName "\\Server50\CapacityManagerReports_Archive\"}
}
}

Resources