如何使用& $ var继续错误调用可执行文件

时间:2016-11-23 10:24:45

标签: powershell

当使用&$executable调用可执行文件时,我想说继续出错。例如:

let urlAsString1 : String = API let urlStr : NSString = urlAsString1.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! let params:NSMutableDictionary? = [ "user_name":"SNSH" as String, "password":"SNOSH" as String, "device_id":"0D4F5322-81C0-0000-9210-70DA0C6BC04C" as String, ]; let ulr = NSURL(string:urlStr as String) let request = NSMutableURLRequest(URL: ulr!) request.HTTPMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") let data = try! NSJSONSerialization.dataWithJSONObject(params!, options: NSJSONWritingOptions.PrettyPrinted) let json = NSString(data: data, encoding: NSUTF8StringEncoding) if let json = json { print(json) } request.HTTPBody = json!.dataUsingEncoding(NSUTF8StringEncoding); Alamofire.request(request) .responseJSON { response in // do whatever you want here switch (response.result) { case .Success(let JSON): print("JSON: \(JSON)") let responseString = JSON as! NSDictionary print(responseString) break; case .Failure: break } } }

我有什么方法可以得到这个吗?

2 个答案:

答案 0 :(得分:3)

考虑使用函数来重置旧的ErrorActionPreference

function Invoke-MyFunctorSilently 
{
    param
    (
        [scriptblock]$func
    )

    $currentEa = $ErrorActionPreference
    try
    {
        $ErrorActionPreference = 'SilentlyContinue'
        $func.Invoke()
    }
    finally
    {
        $ErrorActionPreference = $currentEa
    }
} 

所以你可以像这样调用它:

Invoke-MyFunctorSilently -func { & calc.exe}

答案 1 :(得分:2)

$ErrorActionPreference全局变量设置为以下选项之一:

静默持续,停止,继续,查询,忽略

例如:

$ErrorActionPreference = 'SilentlyContinue'
&$executable