首页 > 解决方案 > 正确地将参数传递给函数

问题描述

伙计们!今天在我的编码冒险中,我试图创建一个依赖用户输入来设置变量的 powershell 脚本。此脚本的主要目标是能够输入计算机名称和网络位置并远程删除域中指定计算机上的文件。请注意,这确实适用于从文本中获取计算机名称。但是,我想避免创建/编辑 .txt 文件,手动编辑函数中的位置,并从主机输入中提取变量。

问题 1.) 这有可能做到吗?问题 2.) 有人可以查看我的代码并提供建议以正确将参数传递给函数吗?

param (
[string] $Computer = $(Read-Host "Copy Paste Computer name"),
[string] $Location = $(Read-Host "Copy Paste Malicious folder/file location starting with Profile 
name")
)

function delete-remotefile {
PROCESS {
            $file = "\\$_\c$\$Location"    
            if (test-path $file)
            {
            echo "$_ file exists"
            Remove-Item $file -force -recurse
            echo "$_ file deleted"
            }
            $file = "\\$_\c$\$Location"          
            if (test-path $file)
            {
            echo "$_ file exists"
            Remove-Item $file -force -recurse
            echo "$_ file deleted"
            }
        }
}

Get-Content $Location | delete-remotefile 

标签: functionpowershellvariables

解决方案


推荐阅读