powershell -replace在被vba调用时不会工作

时间:2015-05-01 15:10:55

标签: regex vba powershell outlook

我有一个powershell脚本,可以从字符串中替换一些charatere。字符串看起来像这样:
1234 - 长项目名称
我需要字符串来替换下划线的空格,所以它看起来像这样:1234_A_Long_Project_Name

这是我的powershell代码的一部分:

...
$projet = $projet -replace '\s+-\s+','_'  
$projet = $projet -replace '\s+|_+','_'
...

当我直接在powershell中运行此脚本时,它可以实现我想要的功能。问题是我需要从Outlook中的VBA宏调用脚本,当收到一个特定主题的邮件时,这个宏被触发:

sText = Split(olItem.Body, vbCrLf)
    Line = Split(sText(1), ":")
    If Line(0) = "Projet " Then
        projet = Trim(Line(1))
        retval = Shell("powershell -noexit c:\script\droit.ps1 '" & projet & "'")
    End If

如果我从powershell运行带有字符串“1234 - Long project Name”的脚本,请给我这个:
1234_Long_Project_Name
当vba使用相同的字符串触发脚本时,它会返回给我:
1234 _-_ Long_Project_Name

任何人都知道为什么它不会给我相同的结果 仅供参考,我在Win 8.1,powerhsell 4和outlook 2013上运行脚本。

谢谢大家

编辑: 这是powershell脚本:

Param([string]$projet)
function getPass2($adminName, $encrypted, $domain){
    $password = convertto-securestring -string $encrypted
    $Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $domain\$AdminName,$password
    $cred
}

$projet = $projet -replace '\s+-\s+','_'
$projet = $projet -replace '\s+|_+','_'
$projet = $projet -replace 'é|è|ê','e'
$projet = $projet -replace 'à','a'
$projet = $projet -replace 'ï','i'
$projet = $projet -replace 'ç','c'
$projet

$encrypted = "123"
$AdminName = "123"

$cred = getPass2 $adminName $encrypted "123"
$exist = $false

#commande for serve1
if ($cred){
    $session = New-PSSession -credential $cred -ComputerName "server"

    #ScriptBlock server1
    $result = Invoke-command -session $session  -Args $projet -ScriptBlock {
        $projet = $args[0]
        #folder copy
        if($projet){
            $SFolder = "z:\FolderTemplate\"
            $DFolder = "z:\DestinationFolder\" + $projet 
            $FileExists = Test-Path $DFolder
            if  ($FileExists -eq $false)
                {
                    Copy-Item $SFolder $DFolder -recurse

                    #acl
                    $SFolderList = get-childitem -name $SFolder -recurse
                    foreach ($Folder in $SFolderList) {
                              $SFullPath = $SFolder + "$Folder"
                              $DFullPath = $DFolder + "\" + "$Folder"
                          $NewACL = Get-ACL "$SFullPath"
                    Set-ACL "$DFullPath" $NewACL
                    }
                }
            else
                {
                    $exist = $true
                    return $exist
                }
        }
        else{
            echo "error"
        }s
    }
    Remove-PsSession -session $session
    $exist = $result


    #Credentiel someserver
    $AdminName = "123"
    $encrypted = "123"
    $credMTL = getPass2 $adminName $encrypted "123"
    #commande for some server
    if ($credMTL -and !$exist){
            $session = New-PSSession -credential $credMTL -ComputerName "server2"  
            $result = Invoke-command -session $session  -Args $projet -ScriptBlock {
                $projet = $args[0]
                $SFolder = "F:\Folder\template"
                $DFolder = "F:\destinationFolder\" + $projet 
                $FileExists = Test-Path $DFolder
                    if  ($FileExists -eq $false)
                        {
                            $shortcutName = $DFolder + "\someLink.lnk"
                            $shortcutTarget = "\\linkPath\" + $projet

                            Copy-Item $SFolder $DFolder -recurse

                            $shell = New-Object -COM WScript.Shell
                            $shortcut = $shell.CreateShortcut($shortcutName)
                            $shortcut.TargetPath = $shortcut.TargetPath + "\" + $projet
                            $shortcut.Description = $projet
                            $shortcut.Save()
                            $exist = $false
                        }
                    else {
                        $exist = $true
                        }
            return $exist
            }
            Remove-PsSession -session $session
    }
    return $result
}

1 个答案:

答案 0 :(得分:0)

好的,我最终发现了什么。当我在outlook中编写测试邮件时,该行被Outlook的自动更正功能更改,而“ - ”被其他内容更改(不知道字符,它看起来像“ - ”但更长一点)。

当我收到来自我的prod machin的自动生成的邮件时,它实际上工作得非常好。

谢谢大家的帮助。