V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sx90
V2EX  ›  自言自语

必须马克, PowerShell function 太坑了

  •  
  •   sx90 · 2020-01-02 23:54:15 +08:00 · 212 次点击
    这是一个创建于 1584 天前的主题,其中的信息可能已经有所发展或是发生改变。

    About Functions


    这个坑,浪费我一天时间,才填上,蛋疼

    先上错误代码:

    #函数
    function Write-Ini  ($IniKey, $Value)
    {
     $Section = "关机";
     $FilePath = "$PSScriptRoot\Site.ini";
     $Null = [IniFileManager]::WriteToIniFile($FilePath, $Section, $IniKey, $Value); 
    };
    
    #调用
    Write-Ini ( "DownloadSpeed", $dlShutdown);
    

    玩了这么多的鹰,自认为这样写没毛病,可惜他狠狠的啄了我一脸,ini 输出为:

    [关机]
    DownloadSpeed 600=
    

    然后就疯狂改代码,疯狂找资料,各种吐血行为,差点想自编一个算了,最后认认真真拜读微软文档后,才发现我调用出错了

    修改后:

    function Write-Ini
    {
      param ([string]$IniKey, [string]$Value);
      $Section = "关机";
      $FilePath = "$PSScriptRoot\Site.ini";
      $Null = [IniFileManager]::WriteToIniFile($FilePath, $Section, $IniKey, $Value); 
    };
    
    Write-Ini -IniKey "DownloadSpeed" -Value $dlShutdown;
    

    撒花完结,睡觉了(两天一夜没闭眼了)

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   755 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:27 · PVG 04:27 · LAX 13:27 · JFK 16:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.