我开始使用PowerShell并在'库'文件中创建函数以提高可读性,然后我从'worker'脚本调用它.
function ShowMessage($AValue)
{
$a = new-object -comobject wscript.shell
$b = $a.popup( $AValue )
}
Run Code Online (Sandbox Code Playgroud)
在PowerShell IDE中运行'worker'脚本时工作正常,但是当我右键单击worker文件并选择'Run with PowerShell'时,它找不到函数'ShowMessage'.两个文件都在同一个文件夹中.请问这里可能会发生什么?
当在包含空格的路径上调用exe时,我遇到了PowerShell的问题.
PS C:\ Windows服务> invoke-expression"C:\ Windows Services\MyService.exe"
术语"C:\ Windows"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
它似乎在"Windows"和"服务"之间的空间上分裂.知道怎么解决这个问题吗?
考虑:
Invoke-WebRequest $sumoApiURL -Headers @{"Content-Type"= "application/json"} -Credential $cred -WebSession $webRequestSession -Method post -Body $sumojson -ErrorAction Stop
Run Code Online (Sandbox Code Playgroud)
这会引发以下异常:
如何完全捕获它或至少过滤掉"已存在同名资源"?
使用$_.Exception.GetType().FullName收益率
System.Net.WebException
并$_.Exception.Message给出
远程服务器返回错误:(400)错误请求.
我想在导入之前删除大约5000个文本文件的第一行.
我仍然是PowerShell的新手,所以不确定要搜索什么或如何处理它.我目前使用伪代码的概念:
set-content file (get-content unless line contains amount)
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法弄清楚如何做像contains这样的事情.
如何在没有空格的情况下将变量写入控制台?我尝试时有问题:
$MyVariable = "Some text"
Write-Host "$MyVariableNOSPACES"
Run Code Online (Sandbox Code Playgroud)
我想要以下输出:
Some textNOSPACES
Run Code Online (Sandbox Code Playgroud) 我正在学习PowerShell,我正在尝试构建自己的模块库.
我写了一个简单的模块XMLHelpers.psm1并放入我的文件夹$home/WindowsPowerShell/Modules.
当我做:
import-module full_path_to_XMLHelpers.psm1
Run Code Online (Sandbox Code Playgroud)
有用.但当我这样做时:
import-module XMLHelpers
Run Code Online (Sandbox Code Playgroud)
它不起作用,我收到错误:
Import-Module:未加载指定的模块"xmlhelpers",因为在任何模块目录中都找不到有效的模块文件.
我检查过环境变量PSModulePath包含这个文件夹.由于它是一个网络文件夹,我还尝试将其移动到本地文件夹并进行修改PSModulePath但没有成功
$env:PSModulePath=$env:PSModulePath+";"+'C:\local'
Run Code Online (Sandbox Code Playgroud)
有什么可能导致这个问题的想法吗?
我正在尝试将文件复制到新位置,维护目录结构.
$source = "c:\some\path\to\a\file.txt"
destination = "c:\a\more\different\path\to\the\file.txt"
Copy-Item $source $destination -Force -Recurse
Run Code Online (Sandbox Code Playgroud)
但我得到一个DirectoryNotFoundException:
Copy-Item : Could not find a part of the path 'c:\a\more\different\path\to\the\file.txt'
Run Code Online (Sandbox Code Playgroud) 如何将此路径与此变量连接起来?
$file = "My file 01.txt" #The file name contains spaces
$readfile = gc "\\server01\folder01\" + ($file) #It doesn't work
Run Code Online (Sandbox Code Playgroud)
谢谢