我试图在Windows 2008 Server上运行脚本,但不确定是否可以运行它。我收到以下错误消息:
Unable to find type [System.Net.Webutility]: make sure that the assembly containing this type is loaded.
如上所述,我试图在2008 Server上运行它。根据System.Net.Webutility文档,它自Windows 8以来一直可用。我是SOL还是有解决方法?
以下是我的代码:
#account of the Shared Mailbox
$account = "MyAccount@myaddress.com"
#date to append to new file name
$date = Get-Date -Format yyyyMMdd
#Formatted date to search Inbox
$searchDate = Get-Date -Format M/dd/yyyy
Write-Host "Searching for $searchDate..."
#file to save attachment as
$newFileName = "Record_Import_$date.html"
#file name for the XML File
$newXMLFileName = "Record_Import_$date.xml"
#File paths Main directory, HTML, HTML Record and Raw folders
$newFilePath = "\\PathA\"
$newHTMLPath = "\\PathB\"
$newHTMLRecordPath = "\\PathC\"
$rawFilePath = "\\PathD\"
#Go into Outlook and get the MAPI
$mail = New-Object -ComObject outlook.application
$mailNS = $mail.GetNamespace("MAPI")
#print out Accounts in MAPI
foreach($email in $mailNS.Folders)
{
Write-Host $email.Name
}
Write-Host "---"
#get the account and Inbox we want
$myAcount = $mailNS.Folders | ? {$_.Name -eq $account}
#Print out folders in Shared Inbox
foreach($folder in $myAcount.Folders)
{
Write-Host $folder.Name
}
#Get the inbox of the account
$myInbox = $myAcount.Folders | ? {$_.Name -eq "Inbox"};
#loop through the Inbox and get any Attachments with the extension of ".EXCEL"
foreach ($f in $myInbox)
{
foreach($i in $f.Items)
{
if($i.ReceivedTime.Date -eq $searchDate)
{
Write-Host "---"
Write-Host "Checking "$i.Subject"..."
Write-Host "---"
foreach($a in $i.Attachments)
{
if($a.FileName -like "*.MYFILETYPE")
{
#Move the attachment to the desired directory and save the raw file in the Raw directory
$a.SaveAsFile((Join-Path $newHTMLPath $newFileName))
$a.SaveAsFile((Join-Path $rawFilePath $a.FileName))
Write-Host $a.FileName " Saved as HTML"
#Open the HTML file and parse it
$htmlString = Get-Content (Join-Path $newHTMLPath $newFileName)
[xml]$html = [System.Net.WebUtility]::HtmlDecode($htmlString);
#Get the date of the HTML File
$dateSpan = $html.DocumentElement.SelectSingleNode('//tr[2]/td[2]').InnerText
#Reset the newXMLFileName and newFileName variables to include the new date
$newXMLFileName = "BAI_Import_$dateSpan.xml"
$newFileName = "BAI_Import_$dateSpan.html"
$a.SaveAsFile((Join-Path $newHTMLRecordPath $newFileName))
Write-Host "Date for XML is $dateSpan"
$xml = @'
<?xml version="1.0" encoding="utf-8" ?>
<Date>
'@;
$xml = $xml += $dateSpan
$xml = $xml += @'
</Date>
<Cash Activities>
'@;
$rows = $html.DocumentElement.SelectNodes('//tr');
$pastVariance = $false
foreach ($row in $rows)
{
if ($row.GetAttribute('class') -eq 'c12')
{
$xml += "`t<Cash Activity>`n";
$spans = $row.SelectNodes('.//descendant::span[@class]');
if ($spans.Count -eq 2)
{
$spanCheck = $spans[0].InnerText.Trim();
Write-Host $spanCheck
if($pastVariance)
{
$xml += "`t`t<Activity>Unknown Cash Activity</Activity>`n";
} Else
{
$xml += "`t`t<Activity>$($spans[0].InnerText.Trim())</Activity>`n";
}
$xml += "`t`t<Balance>$($spans[1].InnerText.Trim())</Balance>`n";
#check to make sure that this line was a Variance
if($spanCheck -match "Variance")
{
$pastVariance = $true
}
}
$xml += "`t</Cash Activity>`n";
}
}
$xml += @'
</Cash Activities>
'@;
Write-Host $xml
$xml | Out-File (Join-Path $newFilePath $newXMLFileName)
Write-Host $i.Subject " Exported to XML File"
}#End of EXCEL Check
}
}
}
}
答案 0 :(得分:1)
System.Web.HttpUtility.HtmlDecode已经可用,因此您可以选择完成相同的操作:
Add-Type -AssemblyName System.Web;
$encoded = '<span class="c2">FRIDAY </span>';
[System.Web.HttpUtility]::HtmlDecode($encoded);
答案 1 :(得分:0)
修复:安装Windows Management Framework 5.1以获取PowerShell 5.1,但请注意,这是系统范围的更改,某些软件(特别是Exchange)不支持更改PowerShell版本。
解决方法:您的代码仅使用它来执行单个HTMLDecode,这不是一个非常复杂的操作;对于.Net Core中的那个方法here is the source code,所以要么在C#中以某种方式内联,要么将其移植到PowerShell。
我做了什么,并且花了我很长的时间 - 让我开始“不是一个非常复杂的操作”,我没有意识到它必须处理多字节unicode,并且有内存性能优化查找表和他们发生冲突的PowerShell字符串处理等;)但这里是:
[char]$HIGH_SURROGATE_START = 0xD800
[char]$LOW_SURROGATE_START = 0xDC00
[char]$LOW_SURROGATE_END = 0xDFFF
[int]$UNICODE_PLANE00_END = 0x00FFFF
[int]$UNICODE_PLANE01_START = 0x10000
[int]$UNICODE_PLANE16_END = 0x10FFFF
function ConvertTo-Uint64Key ([char[]]$entity) {
# The ulong key is the reversed single-byte character representation of the actual entity string.
[uint64]$key = 0
for ($i = 0; $i -lt $entity.Length; $i++)
{
if ($entity[$i] -gt 0xFF)
{
return 0
}
$key = ($key -shl 8) -bor $entity[$i]
}
$key
}
# To avoid an allocation, keys of type "ulong" are used in the lookup table.
# Since all entity strings comprise 8 characters or less and are ASCII-only, they "fit" into an ulong (8 bytes).
# This is a port of the C#, with the keys inlined and the entity code in the comment.
$htmlEntityLookup = @{
[uint64]1903521652 = [char]0x22 # quot
[uint64]6385008 = [char]0x26 # amp
[uint64]1634758515 = [char]0x27 # apos
[uint64]27764 = [char]0x3c # lt
[uint64]26484 = [char]0x3e # gt
[uint64]1851945840 = [char]0xa0 # nbsp
[uint64]452673954668 = [char]0xa1 # iexcl
[uint64]1667591796 = [char]0xa2 # cent
[uint64]482906304100 = [char]0xa3 # pound
[uint64]109356082423150 = [char]0xa4 # curren
[uint64]7955822 = [char]0xa5 # yen
[uint64]108243751952754 = [char]0xa6 # brvbar
[uint64]1936024436 = [char]0xa7 # sect
[uint64]7695724 = [char]0xa8 # uml
[uint64]1668247673 = [char]0xa9 # copy
[uint64]1869767782 = [char]0xaa # ordf
[uint64]465491293551 = [char]0xab # laquo
[uint64]7237492 = [char]0xac # not
[uint64]7563385 = [char]0xad # shy
[uint64]7497063 = [char]0xae # reg
[uint64]1835098994 = [char]0xaf # macr
[uint64]6579559 = [char]0xb0 # deg
[uint64]123611129277806 = [char]0xb1 # plusmn
[uint64]1937076274 = [char]0xb2 # sup2
[uint64]1937076275 = [char]0xb3 # sup3
[uint64]418280469605 = [char]0xb4 # acute
[uint64]469919560303 = [char]0xb5 # micro
[uint64]1885434465 = [char]0xb6 # para
[uint64]120299423297396 = [char]0xb7 # middot
[uint64]426902841708 = [char]0xb8 # cedil
[uint64]1937076273 = [char]0xb9 # sup1
[uint64]1869767789 = [char]0xba # ordm
[uint64]491261097327 = [char]0xbb # raquo
[uint64]112641446195508 = [char]0xbc # frac14
[uint64]112641446195506 = [char]0xbd # frac12
[uint64]112641446196020 = [char]0xbe # frac34
[uint64]115936021803892 = [char]0xbf # iquest
[uint64]71912556426853 = [char]0xc0 # Agrave
[uint64]71886536275045 = [char]0xc1 # Aacute
[uint64]280840729187 = [char]0xc2 # Acirc
[uint64]71968240723045 = [char]0xc3 # Atilde
[uint64]1098214764 = [char]0xc4 # Auml
[uint64]281092386407 = [char]0xc5 # Aring
[uint64]280337607015 = [char]0xc6 # AElig
[uint64]74094181902700 = [char]0xc7 # Ccedil
[uint64]76310602937957 = [char]0xc8 # Egrave
[uint64]76284582786149 = [char]0xc9 # Eacute
[uint64]298020598371 = [char]0xca # Ecirc
[uint64]1165323628 = [char]0xcb # Euml
[uint64]80708649449061 = [char]0xcc # Igrave
[uint64]80682629297253 = [char]0xcd # Iacute
[uint64]315200467555 = [char]0xce # Icirc
[uint64]1232432492 = [char]0xcf # Iuml
[uint64]4543560 = [char]0xd0 # ETH
[uint64]86261891884133 = [char]0xd1 # Ntilde
[uint64]87305719215717 = [char]0xd2 # Ograve
[uint64]87279699063909 = [char]0xd3 # Oacute
[uint64]340970271331 = [char]0xd4 # Ocirc
[uint64]87361403511909 = [char]0xd5 # Otilde
[uint64]1333095788 = [char]0xd6 # Ouml
[uint64]499984983411 = [char]0xd7 # times
[uint64]87357158159208 = [char]0xd8 # Oslash
[uint64]93902788982373 = [char]0xd9 # Ugrave
[uint64]93876768830565 = [char]0xda # Uacute
[uint64]366740075107 = [char]0xdb # Ucirc
[uint64]1433759084 = [char]0xdc # Uuml
[uint64]98274815341669 = [char]0xdd # Yacute
[uint64]361990410830 = [char]0xde # THORN
[uint64]495975164263 = [char]0xdf # szlig
[uint64]107096928515685 = [char]0xe0 # agrave
[uint64]107070908363877 = [char]0xe1 # aacute
[uint64]418279682659 = [char]0xe2 # acirc
[uint64]107152612811877 = [char]0xe3 # atilde
[uint64]1635085676 = [char]0xe4 # auml
[uint64]418531339879 = [char]0xe5 # aring
[uint64]418313431399 = [char]0xe6 # aelig
[uint64]109278553991532 = [char]0xe7 # ccedil
[uint64]111494975026789 = [char]0xe8 # egrave
[uint64]111468954874981 = [char]0xe9 # eacute
[uint64]435459551843 = [char]0xea # ecirc
[uint64]1702194540 = [char]0xeb # euml
[uint64]115893021537893 = [char]0xec # igrave
[uint64]115867001386085 = [char]0xed # iacute
[uint64]452639421027 = [char]0xee # icirc
[uint64]1769303404 = [char]0xef # iuml
[uint64]6648936 = [char]0xf0 # eth
[uint64]121446263972965 = [char]0xf1 # ntilde
[uint64]122490091304549 = [char]0xf2 # ograve
[uint64]122464071152741 = [char]0xf3 # oacute
[uint64]478409224803 = [char]0xf4 # ocirc
[uint64]122545775600741 = [char]0xf5 # otilde
[uint64]1869966700 = [char]0xf6 # ouml
[uint64]110404120962149 = [char]0xf7 # divide
[uint64]122541530248040 = [char]0xf8 # oslash
[uint64]129087161071205 = [char]0xf9 # ugrave
[uint64]129061140919397 = [char]0xfa # uacute
[uint64]504179028579 = [char]0xfb # ucirc
[uint64]1970629996 = [char]0xfc # uuml
[uint64]133459187430501 = [char]0xfd # yacute
[uint64]499968340590 = [char]0xfe # thorn
[uint64]2037738860 = [char]0xff # yuml
[uint64]340467149159 = [char]0x152 # OElig
[uint64]478442973543 = [char]0x153 # oelig
[uint64]91686301757294 = [char]0x160 # Scaron
[uint64]126870673846126 = [char]0x161 # scaron
[uint64]1500867948 = [char]0x178 # Yuml
[uint64]1718513510 = [char]0x192 # fnof
[uint64]1667854947 = [char]0x2c6 # circ
[uint64]499984917605 = [char]0x2dc # tilde
[uint64]280992180321 = [char]0x391 # Alpha
[uint64]1113945185 = [char]0x392 # Beta
[uint64]306577239393 = [char]0x393 # Gamma
[uint64]293759382625 = [char]0x394 # Delta
[uint64]19545414385299310 = [char]0x395 # Epsilon
[uint64]1516598369 = [char]0x396 # Zeta
[uint64]4551777 = [char]0x397 # Eta
[uint64]362528732257 = [char]0x398 # Theta
[uint64]1232041057 = [char]0x399 # Iota
[uint64]323757305953 = [char]0x39a # Kappa
[uint64]83981330703457 = [char]0x39b # Lambda
[uint64]19829 = [char]0x39c # Mu
[uint64]20085 = [char]0x39d # Nu
[uint64]22633 = [char]0x39e # Xi
[uint64]22356822567579502 = [char]0x39f # Omicron
[uint64]20585 = [char]0x3a0 # Pi
[uint64]5400687 = [char]0x3a1 # Rho
[uint64]358250671457 = [char]0x3a3 # Sigma
[uint64]5529973 = [char]0x3a4 # Tau
[uint64]24049014012669806 = [char]0x3a5 # Upsilon
[uint64]5269609 = [char]0x3a6 # Phi
[uint64]4417641 = [char]0x3a7 # Chi
[uint64]5272425 = [char]0x3a8 # Psi
[uint64]341137778529 = [char]0x3a9 # Omega
[uint64]418431133793 = [char]0x3b1 # alpha
[uint64]1650816097 = [char]0x3b2 # beta
[uint64]444016192865 = [char]0x3b3 # gamma
[uint64]431198336097 = [char]0x3b4 # delta
[uint64]28552613640040302 = [char]0x3b5 # epsilon
[uint64]2053469281 = [char]0x3b6 # zeta
[uint64]6648929 = [char]0x3b7 # eta
[uint64]499967685729 = [char]0x3b8 # theta
[uint64]1768911969 = [char]0x3b9 # iota
[uint64]461196259425 = [char]0x3ba # kappa
[uint64]119165702792289 = [char]0x3bb # lambda
[uint64]28021 = [char]0x3bc # mu
[uint64]28277 = [char]0x3bd # nu
[uint64]30825 = [char]0x3be # xi
[uint64]31364021822320494 = [char]0x3bf # omicron
[uint64]28777 = [char]0x3c0 # pi
[uint64]7497839 = [char]0x3c1 # rho
[uint64]126896543981926 = [char]0x3c2 # sigmaf
[uint64]495689624929 = [char]0x3c3 # sigma
[uint64]7627125 = [char]0x3c4 # tau
[uint64]33056213267410798 = [char]0x3c5 # upsilon
[uint64]7366761 = [char]0x3c6 # phi
[uint64]6514793 = [char]0x3c7 # chi
[uint64]7369577 = [char]0x3c8 # psi
[uint64]478576732001 = [char]0x3c9 # omega
[uint64]8388065856503118189 = [char]0x3d1 # thetasym
[uint64]504397785448 = [char]0x3d2 # upsih
[uint64]7367030 = [char]0x3d6 # piv
[uint64]1701737328 = [char]0x2002 # ensp
[uint64]1701671792 = [char]0x2003 # emsp
[uint64]127991794266992 = [char]0x2009 # thinsp
[uint64]2054647402 = [char]0x200c # zwnj
[uint64]8025962 = [char]0x200d # zwj
[uint64]7107181 = [char]0x200e # lrm
[uint64]7498861 = [char]0x200f # rlm
[uint64]474130510696 = [char]0x2013 # ndash
[uint64]469835543400 = [char]0x2014 # mdash
[uint64]465793283439 = [char]0x2018 # lsquo
[uint64]491563087215 = [char]0x2019 # rsquo
[uint64]495572841839 = [char]0x201a # sbquo
[uint64]465541625199 = [char]0x201c # ldquo
[uint64]491311428975 = [char]0x201d # rdquo
[uint64]422591952239 = [char]0x201e # bdquo
[uint64]110369509434738 = [char]0x2020 # dagger
[uint64]75185137345906 = [char]0x2021 # Dagger
[uint64]1651862636 = [char]0x2022 # bull
[uint64]114784820029808 = [char]0x2026 # hellip
[uint64]123581013780844 = [char]0x2030 # permil
[uint64]482955849061 = [char]0x2032 # prime
[uint64]345516895589 = [char]0x2033 # Prime
[uint64]119242811864431 = [char]0x2039 # lsaquo
[uint64]125839881631087 = [char]0x203a # rsaquo
[uint64]478560218725 = [char]0x203e # oline
[uint64]440005653356 = [char]0x2044 # frasl
[uint64]1702195823 = [char]0x20ac # euro
[uint64]452806666085 = [char]0x2111 # image
[uint64]131277443658352 = [char]0x2118 # weierp
[uint64]1919246700 = [char]0x211c # real
[uint64]500135191653 = [char]0x2122 # trade
[uint64]27422255507274093 = [char]0x2135 # alefsym
[uint64]1818325618 = [char]0x2190 # larr
[uint64]1969320562 = [char]0x2191 # uarr
[uint64]1918988914 = [char]0x2192 # rarr
[uint64]1684107890 = [char]0x2193 # darr
[uint64]1751216754 = [char]0x2194 # harr
[uint64]427120751218 = [char]0x21b5 # crarr
[uint64]1816228466 = [char]0x21d0 # lArr
[uint64]1967223410 = [char]0x21d1 # uArr
[uint64]1916891762 = [char]0x21d2 # rArr
[uint64]1682010738 = [char]0x21d3 # dArr
[uint64]1749119602 = [char]0x21d4 # hArr
[uint64]112628846390380 = [char]0x2200 # forall
[uint64]1885434484 = [char]0x2202 # part
[uint64]435811873652 = [char]0x2203 # exist
[uint64]435627783289 = [char]0x2205 # empty
[uint64]474080242785 = [char]0x2207 # nabla
[uint64]1769171310 = [char]0x2208 # isin
[uint64]474316302702 = [char]0x2209 # notin
[uint64]28265 = [char]0x220b # ni
[uint64]1886547812 = [char]0x220f # prod
[uint64]7566701 = [char]0x2211 # sum
[uint64]469920281971 = [char]0x2212 # minus
[uint64]119226000044916 = [char]0x2217 # lowast
[uint64]491260242275 = [char]0x221a # radic
[uint64]1886547824 = [char]0x221d # prop
[uint64]452823771502 = [char]0x221e # infin
[uint64]6385255 = [char]0x2220 # ang
[uint64]6385252 = [char]0x2227 # and
[uint64]28530 = [char]0x2228 # or
[uint64]6513008 = [char]0x2229 # cap
[uint64]6518128 = [char]0x222a # cup
[uint64]6909556 = [char]0x222b # int
[uint64]127991727416628 = [char]0x2234 # there4
[uint64]7563629 = [char]0x223c # sim
[uint64]1668247143 = [char]0x2245 # cong
[uint64]418549165424 = [char]0x2248 # asymp
[uint64]28261 = [char]0x2260 # ne
[uint64]435695217014 = [char]0x2261 # equiv
[uint64]27749 = [char]0x2264 # le
[uint64]26469 = [char]0x2265 # ge
[uint64]7566690 = [char]0x2282 # sub
[uint64]7566704 = [char]0x2283 # sup
[uint64]1853060450 = [char]0x2284 # nsub
[uint64]1937072741 = [char]0x2286 # sube
[uint64]1937076325 = [char]0x2287 # supe
[uint64]478627526003 = [char]0x2295 # oplus
[uint64]122545775666547 = [char]0x2297 # otimes
[uint64]1885696624 = [char]0x22a5 # perp
[uint64]1935961972 = [char]0x22c5 # sdot
[uint64]465524058476 = [char]0x2308 # lceil
[uint64]491293862252 = [char]0x2309 # rceil
[uint64]119187161706354 = [char]0x230a # lfloor
[uint64]125784231473010 = [char]0x230b # rfloor
[uint64]1818324583 = [char]0x2329 # lang
[uint64]1918987879 = [char]0x232a # rang
[uint64]7106426 = [char]0x25ca # loz
[uint64]126926507500915 = [char]0x2660 # spades
[uint64]427021394547 = [char]0x2663 # clubs
[uint64]114784635876467 = [char]0x2665 # hearts
[uint64]431264722291 = [char]0x2666 # diams
}
function ConvertFrom-EncodedHTML ($Text)
{
[regex]::Replace($Text, '&(.{1,8});', {
param($match)
$entityCode = $match.Groups[1].Value
switch ($entityCode)
{
# numeric entity
# The # syntax can be in decimal or hex, e.g.
# å --> decimal
# å --> same char in hex
# See http://www.w3.org/TR/REC-html40/charset.html#entities
{$entityCode[0] -eq '#'} {
[uint32]$parsedValue = 0
if ($entityCode[1] -eq 'x')
{
$parsedSuccessfully = [uint32]::TryParse($entityCode.SubString(2), [System.Globalization.NumberStyles]::AllowHexSpecifier, [CultureInfo]::InvariantCulture, [ref]$parsedValue)
}
else
{
$parsedSuccessfully = [uint32]::TryParse($entityCode.SubString(1), [System.Globalization.NumberStyles]::Integer, [CultureInfo]::InvariantCulture, [ref]$parsedValue)
}
if ($parsedSuccessfully)
{
# decoded character must be U+0000 .. U+10FFFF, excluding surrogates
$parsedSuccessfully = (($parsedValue -lt $HIGH_SURROGATE_START) -or ([int]$LOW_SURROGATE_END -lt $parsedValue -and $parsedValue -le $UNICODE_PLANE16_END))
}
if ($parsedSuccessfully)
{
if ($parsedValue -le $UNICODE_PLANE00_END)
{
# single character
[char]$parsedValue
}
else
{
# multi-character
# "Convert supplementary multilingual plane to utf16"
# similar to Char.ConvertFromUtf32, but doesn't check arguments or generate strings
# input is assumed to be an SMP character
[int] $utf32 = ($parsedValue - $UNICODE_PLANE01_START)
$remainder = 0
[char]$leadingSurrogate = ([math]::DivRem($utf32, 0x400, [ref]$remainder) + $HIGH_SURROGATE_START)
[char]$trailingSurrogate = ($remainder + $LOW_SURROGATE_START)
'{0}{1}' -f $leadingSurrogate, $trailingSurrogate
}
}
}
default {
$entityChar = $htmlEntityLookup[[uint64](ConvertTo-Uint64Key $entityCode)]
if ($entityChar -and $entityChar -ne [char]0)
{
# return looked up char
return $entityChar
}
# return it as original text
"&$entityCode;"
}
}
})
}
然后
ConvertFrom-EncodedHTML 'o with combined diacritic õ, euro €.'
ConvertFrom-EncodedHTML 'This < that > the other'
ConvertFrom-EncodedHTML 'test &nochange; old italic multibyte character in the supplementary multilingual plane 𐌀 𐌁'
变为
o with combined diacritic õ, euro €.
This < that > the other
test &nochange; old italic multibyte character in the supplementary multilingual plane
你或许可以:
[xml]$html = ConvertFrom-EncodedHTML $htmlString