0
에 출력을 추가합니다.
다음 스크립트가 있지만 write-host
을 바꾸고 출력을 저장해야합니다. 출력을 쓰는 대신 단일 변수를 출력해야합니다. 모든 출력을 1 개의 변수로 끝내고 싶습니다. 의 내용을 전자 메일로 보낼 수 있습니다.Powershell 스크립트가 변수
write-host $machines2
은 루프의 끝에 각 시스템을 콘솔에 나열합니다.
# Start writing headers to output
write-host " MACHINE USER TAG EXPIRY DATE"
# Import SQL module for powershell
import-module SQLPS -DisableNameChecking
# Run first SQLquery to get machine name & servie tag
$data1 = Invoke-Sqlcmd -ServerInstance SERVER -Database DATABASE -Query “SELECT guid, name, [Serial Number] FROM dbo.wrksta as a inner join Inv_AeX_HW_Serial_Number as b on a.GUID=b._resourceGUID ORDER BY Name”;
# Iterate through restults and apply logic to populate list
foreach ($row in $data1)
{
# Set variables from Query result
$name = $row.name;
$serial = $row."Serial Number";
# Get machine user
$user = invoke-sqlcmd -ServerInstance SERVER -Database DATABASE -Query “SELECT top 1 [User] FROM dbo.Evt_aex_client_logon WHERE _resourceGuid = (SELECT top 1 Guid FROM dbo.Wrksta WHERE Name='$name' ORDER BY WrkstaID DESC) AND Event = 'logon' AND _eventTime > getdate()-60 GROUP BY [User] ORDER BY count([User]) desc” ;
# Set machine user without column header
$user2=$user.User;
# Set service tag from query data
[String]$ServiceTag = $serial;
Try
{
# Function to obtain XML info about dell system from a service tag from Dell xserv site
$AssetService = New-WebServiceProxy -Uri "http://xserv.dell.com/services/AssetService.asmx?WSDL";
$ApplicationName = "AssetService";
$Guid = [Guid]::NewGuid();
$Asset = $AssetService.GetAssetInformation($Guid,$ApplicationName,$ServiceTag);
$Writer = New-Object "System.IO.StringWriter";
$XmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($Asset.GetType());
$XmlSerializer.Serialize($Writer,$Asset);
[String]$Date = $Writer.ToString();
$Writer.Flush();
$Writer.Close();
}
# Required exception block
Catch
{
Write-Host $($_.Exception.Message);
}
# Match purchase date from returned data in incorrect format 2012-03-23
$prog = [regex]::match($Date,'(?<=StartDate>)(.*)(?=T00)').Groups[1].Value
# Edit the date to replace the - with/
$prog2 = [System.Text.RegularExpressions.Regex]::Replace($prog,"[-]","/");
if ($prog2)
{
# Parse the value e.g. 2012/03/23 into a full datetime i.e. 23 March 2012 12:00:00
$dt = [datetime]::ParseExact($prog2,"yyyy/MM/dd",$null)
# Now string is in datetime format use the -format command to re-order and trim to our desired date 23/03/2012
$purchased = Get-Date $dt -Format 'dd/MM/yyyy'
# Store all variables in a table with relevant column headings
$machines = New-Object PSObject -Property @{Name = $name; Serial = $Serial; User2 = $user2; purchased = $purchased}
# Set what the date was 4 years ago today
$fourYearsAgo = (Get-Date).AddYears(-4)
# Compare our returned date to the date 4 years ago and see if it is less than or equal too i.e. in warranty
$tobereplaced = $machines.purchased | Where-Object { (Get-Date $machines.purchased) -le $fourYearsAgo}
if ($tobereplaced)
{
# Parse the date back to a full date/time
$dt2 = [datetime]::ParseExact($tobereplaced,"dd/MM/yyyy",$null)
# Add 4 years onto the date
$replacementdate = (get-date $dt2).AddYears(+4)
# Trim it back to our desired format
$tbr = Get-Date $replacementdate -Format 'dd/MM/yyyy'
# Build our final variable containing our data
"EXPIRED" + " " + $name + " " + $user2 + " " + $serial + " " + $tbr;
}
}
}
위 코드에 어떻게 적용해야할지 모르시겠습니까? 위의 코드에서 많은 변수가 설정되어 있지만 루프의 각 레코드에서 $ machines2 결과 만 함수에 반환됩니다. – meeeeeeeeee
출력에'$ machines2'를 그냥 사용하려면 해당 행을 EXPIRED로 바꾸십시오 "+ $ name +" "+ $ user2 +" "+ $ serial +" "+ $ tbr' ('$ machines2 ='를 제거하십시오. – Neolisk