下午好,
有人可以告诉我如何使用powershell将日期20120624转换为yyyyMMdd,因为我得到了#34;字符串未被识别为valud日期时间"运行以下代码时
[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)
答案 0 :(得分:3)
更改
[System.Text.RegularExpressions.Regex]::Replace($prog,"[-]","");
到
$prog = [System.Text.RegularExpressions.Regex]::Replace($prog,"[-]","");
您所做的只是返回替换所做的事情,您从未将其实际分配给变量。当我按上述方式运行时,我得到了以下内容:
Sunday, June 24, 2012 12:00:00 AM
答案 1 :(得分:1)
您可以将结果转换为XML而不是String,并避免字符串解析:
[xml]$Result = $Writer.ToString()
[datetime]$Result.ArrayOfAsset.Asset.Entitlements.EntitlementData[0].StartDate