[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] PowerShell如何把重复的代码定义为一个函数

下面代码的红, 蓝字部分, 都是重复的, 怎样定义为一个函数,
例如定义为 ifsuccess

红色字地方, 使用 ifsuccess glob
蓝色字地方, 使用 ifsuccess zip


$glob = New-Object Chilkat.Global
$success = $glob.UnlockBundle("Anything")
if ($success -ne $true)
{
        $($glob.LastErrorText)
        exit
}


$zip = New-Object Chilkat.Zip

$success = $zip.NewZip("test.zip")
if ($success -ne $true)
{
        $($zip.LastErrorText)
        exit
}


$recurse = $true
$success = $zip.AppendFiles("c:/temp/a/*", $recurse)
if ($success -ne $true)
{
        $($zip.LastErrorText)
        exit
}


$success = $zip.WriteZipAndClose()
if ($success -ne $true)
{
        $($zip.LastErrorText)
        exit
}


$("Zip Created!")

  1. function ifsuccess{
  2. if ($success -ne $true)
  3. {
  4.         $(iex('$'+$args+'.LastErrorText'))
  5.         exit
  6. }
  7. }
复制代码
1

评分人数

    • 5i365: 牛X, 乐于助人技术 + 1

TOP

回复 2# idwma


    还真能生效, 大侠 牛X

TOP

回复 2# idwma


    为什么加个 iex 呢? 下面这样写好理解, 但是只会输出   $zip.LastErrorText
  1. function ifsuccess{
  2. if ($success -ne $true)
  3. {
  4.         $('$'+$args+'.LastErrorText')
  5.         exit
  6. }
  7. }
复制代码

TOP

回复 4# 5i365


    help iex帮助里有说明

TOP

返回列表