如何将Invoke RestMethod替换为PowerShell 2.0
我在PowerShell 5.1中创建了一个脚本,用于检索当天未过期的邮件,并将其附件保存到本地驱动器中。问题是,在生产环境中,我只有Powershell 2.0。我在代码中使用Invoke rest方法,如下所示:如何将Invoke RestMethod替换为PowerShell 2.0,powershell,Powershell,我在PowerShell 5.1中创建了一个脚本,用于检索当天未过期的邮件,并将其附件保存到本地驱动器中。问题是,在生产环境中,我只有Powershell 2.0。我在代码中使用Invoke rest方法,如下所示: $url = "https://outlook.office365.com/api/v1.0/me/messages" $date = (get-date).AddDays(-1).ToString("yyyy-MM-dd") $subject = "'report'" $mes
$url = "https://outlook.office365.com/api/v1.0/me/messages"
$date = (get-date).AddDays(-1).ToString("yyyy-MM-dd")
$subject = "'report'"
$messageQuery = "" + $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived ge " + $date + " and Subject eq " + $subject
$messages = Invoke-RestMethod $messageQuery -Credential $cred
foreach ($message in $messages.value)
{
$query = $url + "/" + $message.Id + "/attachments"
$attachments = Invoke-RestMethod $query -Credential $cred
foreach ($attachment in $attachments.value)
{
$attachment.Name
# SAVE ATTACHMENT CODE HERE
}
}
是否有一种简单的方法来转换代码以适合PowerShell 2.0
Invoke-WebRequest
此命令与Invoke RestMethod
基本相同,只是在接收数据后如何处理数据。您将不得不对解析数据的方式进行一些小的修改
我打赌您正在接收JSON数据,因此您只需运行Invoke WebRequest
命令并将其传输到ConvertFrom JSON
并将结果分配给一个变量。这样您就可以执行类似$x.messages |%{$}
您需要在2.0中实现此转换器。您可以从以下位置复制和粘贴:
PS 2.0本机支持XML。谢谢你的回答,但我不知道如何正确使用它,你能给我举个例子吗?