Reputation: 351
Good afternoon,
Can someone tell me how to convert the date 20120624 into yyyyMMdd using powershell as I get "String was not recognised as a valud datetime" when running the following code
[String]$ServiceTag = "b26ybt1";
Try{
$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]$Result = $Writer.ToString();
$Writer.Flush();
$Writer.Close();
Return $Result;
}
Catch{
Write-Host $($_.Exception.Message);
}
$prog = [regex]::match($Result,'(?<=StartDate>)(.*)(?=T00)').Groups[1].Value
[System.Text.RegularExpressions.Regex]::Replace($prog,"[-]","");
[datetime]::ParseExact($prog,"yyyyMMdd",$null)
Upvotes: 1
Views: 2676
Reputation: 126722
You could convert the result to XML instead of a String and avoid string parsing:
[xml]$Result = $Writer.ToString()
[datetime]$Result.ArrayOfAsset.Asset.Entitlements.EntitlementData[0].StartDate
Upvotes: 1
Reputation: 4362
Change
[System.Text.RegularExpressions.Regex]::Replace($prog,"[-]","");
to
$prog = [System.Text.RegularExpressions.Regex]::Replace($prog,"[-]","");
All you did was return what the replacement would do, you never actually assigned it to a variable. When I ran it as above, I got the following:
Sunday, June 24, 2012 12:00:00 AM
Upvotes: 3