首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >修改powershell脚本以添加其他参数

修改powershell脚本以添加其他参数
EN

Stack Overflow用户
提问于 2020-07-22 20:19:01
回答 1查看 29关注 0票数 0

下面的脚本是由我们以前的IT人员创建的,这个脚本的作用基本上是替换登录用户的Appdata中的Funduc Software (RSP和RSBE)配置文件,以便我们可以修改软件将搜索的内容。例如,Excel、Word等。

我需要修改这个脚本,使它在没有任何参数的情况下运行,但如果我们指定了一个参数,它会自动运行我们需要的配置。

现在,如果我们运行.\RSUpdateConfigFile.ps1,它会启动脚本,等待用户输入与我们需要搜索的分机相对应的数字,这样就可以很好地工作。

我们希望它做的是,如果我们运行.\RSUpdateConfigFile.ps1 -L_V_1_String_PreSelection 10,它应该执行脚本,而不会提示用户自动更新带有Word扩展名的配置文件。

下面是我们使用的代码。任何帮助或建议都将不胜感激。

代码语言:javascript
运行
复制
Param(
    $L_V_1_String_DisplayComputerName = $env:ComputerName,
    $L_V_1_String_DestinationPath = $env:APPDATA,
    $L_V_Array_String_ConfigurationFilesToCopy = @(
        "A:\Input\Replace Studio.cfg",
        "A:\Input\Replace Studio Business Edition.cfg"
    ),  
    $L_V_HashTable_String_FileExtensions = [Ordered] @{
        "All                                                       files" = "*.*";
        "All Excel, PowerPoint, Visio, and Word                    files" = "*.xlm;*.xls;*.xlt;*.xlsm;*.xltm;*.xlsx;*.xltx;*.ppt;*.pps;*.pot;*.pptm;*.ppsm;*.potm;*.pptx;*.ppsx;*.potx;*.vsl;*.vsd;*.vsdx;*.vsdm;*.vssm;*.vstm;*.vssx;*.vstx;*.vss;*.vtt;*.vsw;*.vdx;*.vsx;*.vtx;*.doc;*.dot;*.docm;*.dotm;*.docx;*.dotx";
        "All Excel, PowerPoint, Visio, and Word internal           files" = "*_A_I_*.xlm;*_A_I_*.xls;*_A_I_*.xlt;*_A_I_*.xlsm;*_A_I_*.xltm;*_A_I_*.xlsx;*_A_I_*.xltx;*_A_I_*.ppt;*_A_I_*.pps;*_A_I_*.pot;*_A_I_*.pptm;*_A_I_*.ppsm;*_A_I_*.potm;*_A_I_*.pptx;*_A_I_*.ppsx;*_A_I_*.potx;*_A_I_*.vsl;*_A_I_*.vsd;*_A_I_*.vsdx;*_A_I_*.vsdm;*_A_I_*.vssm;*_A_I_*.vstm;*_A_I_*.vssx;*_A_I_*.vstx;*_A_I_*.vss;*_A_I_*.vtt;*_A_I_*.vsw;*_A_I_*.vdx;*_A_I_*.vsx;*_A_I_*.vtx;*_A_I_*.doc;*_A_I_*.dot;*_A_I_*.docm;*_A_I_*.dotm;*_A_I_*.docx;*_A_I_*.dotx";
        "All Excel                                                 files" = "*.xlm;*.xls;*.xlt;*.xlsm;*.xltm;*.xlsx;*.xltx";
        "All Excel                              internal           files" = "*_A_I_*.xlm;*_A_I_*.xls;*_A_I_*.xlt;*_A_I_*.xlsm;*_A_I_*.xltm;*_A_I_*.xlsx;*_A_I_*.xltx";
        "All PowerPoint                                            files" = "*.ppt;*.pps;*.pot;*.pptm;*.ppsm;*.potm;*.pptx;*.ppsx;*.potx";
        "All PowerPoint                         internal           files" = "*_A_I_*.ppt;*_A_I_*.pps;*_A_I_*.pot;*_A_I_*.pptm;*_A_I_*.ppsm;*_A_I_*.potm;*_A_I_*.pptx;*_A_I_*.ppsx;*_A_I_*.potx";
        "All Visio                                                 files" = "*.vsl;*.vsd;*.vsdx;*.vsdm;*.vssm;*.vstm;*.vssx;*.vstx;*.vss;*.vtt;*.vsw;*.vdx;*.vsx;*.vtx";
        "All Visio                              internal           files" = "*_A_I_*.vsl;*_A_I_*.vsd;*_A_I_*.vsdx;*_A_I_*.vsdm;*_A_I_*.vssm;*_A_I_*.vstm;*_A_I_*.vssx;*_A_I_*.vstx;*_A_I_*.vss;*_A_I_*.vtt;*_A_I_*.vsw;*_A_I_*.vdx;*_A_I_*.vsx;*_A_I_*.vtx";
        "All Word                                                  files" = "*.doc;*.dot;*.docm;*.dotm;*.docx;*.dotx";
        "All Word                               internal           files" = "*_A_I_*.doc;*_A_I_*.dot;*_A_I_*.docm;*_A_I_*.dotm;*_A_I_*.docx;*_A_I_*.dotx";
        "All Word                                        agreement files" = "*_A_*.doc;*_A_*.dot;*_A_*.docm;*_A_*.dotm;*_A_*.docx;*_A_*.dotx"
        "All Word                                        form      files" = "*_F_*.doc;*_F_*.dot;*_F_*.docm;*_F_*.dotm;*_F_*.docx;*_F_*.dotx"
        "All Word                                        letter    files" = "*_L_*.doc;*_L_*.dot;*_L_*.docm;*_L_*.dotm;*_L_*.docx;*_L_*.dotx"
    },
    $L_V_1_String_Placeholder = "%Placeholder%"
)
Clear-Host
$L_V_1_Integer_Index = 0
$L_V_1_String_Prompt = "Enter the number representing your desired option:`n`n"
foreach ($L_V_1_String_Key in ($L_V_HashTable_String_FileExtensions.Keys))
{
    $L_V_1_String_Prompt += "$L_V_1_Integer_Index) "+$L_V_1_String_Key+"`n`n"
    $L_V_1_Integer_Index += 1
}
Try
{
    $L_V_1_String_UserSelection = Read-Host -Prompt $L_V_1_String_Prompt
    if (($L_V_1_String_UserSelection -eq $null) -or (-not($L_V_1_String_UserSelection  -match "\d")))
    {
        Throw
    }
    else
    {
        Try
        {
            #Replace placeholders, and save files to destination.
            foreach($L_V_1_String_ConfigurationFileToCopy in $L_V_Array_String_ConfigurationFilesToCopy)
            {
                $L_V_1_FileInfo_ConfigurationFileToCopy = [io.FileInfo] $L_V_1_String_ConfigurationFileToCopy
                $L_V_1_DestinationFilePath = "$L_V_1_String_DestinationPath\"+$L_V_1_FileInfo_ConfigurationFileToCopy.Name
                Get-Content $L_V_1_FileInfo_ConfigurationFileToCopy -ErrorAction Stop | Foreach-Object {$_.replace($L_V_1_String_Placeholder , $L_V_HashTable_String_FileExtensions[[int]$L_V_1_String_UserSelection])} | Set-Content -Path $L_V_1_DestinationFilePath -ErrorAction Stop   
                Write-Host "File '$L_V_1_DestinationFilePath' has been copied!"
            }
        }
        Catch
        {
            Write-Host $_.Exception.Message
            Exit 1
        }
    }
}
Catch
{
    Write-Host "That is not a valid choice!"
    Exit 1
}
Write-Host "ComputerName: $L_V_1_String_DisplayComputerName"
Pause
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-22 21:34:12

我在脚本中添加了你的参数,只有在没有提供的时候才会提示你:

代码语言:javascript
运行
复制
Param(
    $L_V_1_String_DisplayComputerName = $env:ComputerName,
    $L_V_1_String_DestinationPath = $env:APPDATA,
    $L_V_Array_String_ConfigurationFilesToCopy = @(
        "A:\Input\Replace Studio.cfg",
        "A:\Input\Replace Studio Business Edition.cfg"
    ),  
    $L_V_HashTable_String_FileExtensions = [Ordered] @{
        "All                                                       files" = "*.*";
        "All Excel, PowerPoint, Visio, and Word                    files" = "*.xlm;*.xls;*.xlt;*.xlsm;*.xltm;*.xlsx;*.xltx;*.ppt;*.pps;*.pot;*.pptm;*.ppsm;*.potm;*.pptx;*.ppsx;*.potx;*.vsl;*.vsd;*.vsdx;*.vsdm;*.vssm;*.vstm;*.vssx;*.vstx;*.vss;*.vtt;*.vsw;*.vdx;*.vsx;*.vtx;*.doc;*.dot;*.docm;*.dotm;*.docx;*.dotx";
        "All Excel, PowerPoint, Visio, and Word internal           files" = "*_A_I_*.xlm;*_A_I_*.xls;*_A_I_*.xlt;*_A_I_*.xlsm;*_A_I_*.xltm;*_A_I_*.xlsx;*_A_I_*.xltx;*_A_I_*.ppt;*_A_I_*.pps;*_A_I_*.pot;*_A_I_*.pptm;*_A_I_*.ppsm;*_A_I_*.potm;*_A_I_*.pptx;*_A_I_*.ppsx;*_A_I_*.potx;*_A_I_*.vsl;*_A_I_*.vsd;*_A_I_*.vsdx;*_A_I_*.vsdm;*_A_I_*.vssm;*_A_I_*.vstm;*_A_I_*.vssx;*_A_I_*.vstx;*_A_I_*.vss;*_A_I_*.vtt;*_A_I_*.vsw;*_A_I_*.vdx;*_A_I_*.vsx;*_A_I_*.vtx;*_A_I_*.doc;*_A_I_*.dot;*_A_I_*.docm;*_A_I_*.dotm;*_A_I_*.docx;*_A_I_*.dotx";
        "All Excel                                                 files" = "*.xlm;*.xls;*.xlt;*.xlsm;*.xltm;*.xlsx;*.xltx";
        "All Excel                              internal           files" = "*_A_I_*.xlm;*_A_I_*.xls;*_A_I_*.xlt;*_A_I_*.xlsm;*_A_I_*.xltm;*_A_I_*.xlsx;*_A_I_*.xltx";
        "All PowerPoint                                            files" = "*.ppt;*.pps;*.pot;*.pptm;*.ppsm;*.potm;*.pptx;*.ppsx;*.potx";
        "All PowerPoint                         internal           files" = "*_A_I_*.ppt;*_A_I_*.pps;*_A_I_*.pot;*_A_I_*.pptm;*_A_I_*.ppsm;*_A_I_*.potm;*_A_I_*.pptx;*_A_I_*.ppsx;*_A_I_*.potx";
        "All Visio                                                 files" = "*.vsl;*.vsd;*.vsdx;*.vsdm;*.vssm;*.vstm;*.vssx;*.vstx;*.vss;*.vtt;*.vsw;*.vdx;*.vsx;*.vtx";
        "All Visio                              internal           files" = "*_A_I_*.vsl;*_A_I_*.vsd;*_A_I_*.vsdx;*_A_I_*.vsdm;*_A_I_*.vssm;*_A_I_*.vstm;*_A_I_*.vssx;*_A_I_*.vstx;*_A_I_*.vss;*_A_I_*.vtt;*_A_I_*.vsw;*_A_I_*.vdx;*_A_I_*.vsx;*_A_I_*.vtx";
        "All Word                                                  files" = "*.doc;*.dot;*.docm;*.dotm;*.docx;*.dotx";
        "All Word                               internal           files" = "*_A_I_*.doc;*_A_I_*.dot;*_A_I_*.docm;*_A_I_*.dotm;*_A_I_*.docx;*_A_I_*.dotx";
        "All Word                                        agreement files" = "*_A_*.doc;*_A_*.dot;*_A_*.docm;*_A_*.dotm;*_A_*.docx;*_A_*.dotx"
        "All Word                                        form      files" = "*_F_*.doc;*_F_*.dot;*_F_*.docm;*_F_*.dotm;*_F_*.docx;*_F_*.dotx"
        "All Word                                        letter    files" = "*_L_*.doc;*_L_*.dot;*_L_*.docm;*_L_*.dotm;*_L_*.docx;*_L_*.dotx"
    },
    $L_V_1_String_Placeholder = "%Placeholder%",
    $L_V_1_String_PreSelection
)
Clear-Host
$L_V_1_Integer_Index = 0
$L_V_1_String_Prompt = "Enter the number representing your desired option:`n`n"
foreach ($L_V_1_String_Key in ($L_V_HashTable_String_FileExtensions.Keys))
{
    $L_V_1_String_Prompt += "$L_V_1_Integer_Index) "+$L_V_1_String_Key+"`n`n"
    $L_V_1_Integer_Index += 1
}
Try
{
    if ($L_V_1_String_PreSelection){
        $L_V_1_String_UserSelection = $L_V_1_String_PreSelection
    } else {
        $L_V_1_String_UserSelection = Read-Host -Prompt $L_V_1_String_Prompt
    } 
        
    if (($L_V_1_String_UserSelection -eq $null) -or (-not($L_V_1_String_UserSelection  -match "\d")))
    {
        Throw
    }
    else
    {
        Try
        {
            #Replace placeholders, and save files to destination.
            foreach($L_V_1_String_ConfigurationFileToCopy in $L_V_Array_String_ConfigurationFilesToCopy)
            {
                $L_V_1_FileInfo_ConfigurationFileToCopy = [io.FileInfo] $L_V_1_String_ConfigurationFileToCopy
                $L_V_1_DestinationFilePath = "$L_V_1_String_DestinationPath\"+$L_V_1_FileInfo_ConfigurationFileToCopy.Name
                Get-Content $L_V_1_FileInfo_ConfigurationFileToCopy -ErrorAction Stop | Foreach-Object {$_.replace($L_V_1_String_Placeholder , $L_V_HashTable_String_FileExtensions[[int]$L_V_1_String_UserSelection])} | Set-Content -Path $L_V_1_DestinationFilePath -ErrorAction Stop   
                Write-Host "File '$L_V_1_DestinationFilePath' has been copied!"
            }
        }
        Catch
        {
            Write-Host $_.Exception.Message
            Exit 1
        }
    }
}
Catch
{
    Write-Host "That is not a valid choice!"
    Exit 1
}
Write-Host "ComputerName: $L_V_1_String_DisplayComputerName"
Pause
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63034047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档