Powershell: A positional parameter cannot be found that accepts argument

huangapple go评论67阅读模式
英文:

Powershell: A positional parameter cannot be found that accepts argument

问题

在Windows启动期间运行PS脚本遇到了问题。我一直收到以下错误消息。
2023/06/07 16:32:45 GCEMetadataScripts:启动启动脚本(版本20230403.00)。
2023/06/07 16:32:45 GCEMetadataScripts:在元数据中找到了windows-startup-script-ps1。
2023/06/07 16:35:05 GCEMetadataScripts:windows-startup-script-ps1:Set-NetIPAddress:找不到可接受参数'4'的位置参数。
2023/06/07 16:35:05 GCEMetadataScripts:windows-startup-script-ps1:位于C:\Windows\TEMP\metadata-scripts3738309189\windows-startup-script-ps1.ps1:8字符:1

2023/06/07 16:35:05 GCEMetadataScripts:windows-startup-script-ps1:+ Set-NetIPAddress -InterfaceIndex (Get-NetAdapter).InterfaceIndex �?"I ...
2023/06/07 16:35:05 GCEMetadataScripts:windows-startup-script-ps1:+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2023/06/07 16:35:05 GCEMetadataScripts:windows-startup-script-ps1:+ CategoryInfo:InvalidArgument: (:) [Set-NetIPAddress],ParameterBindingException
2023/06/07 16:35:05 GCEMetadataScripts:windows-startup-script-ps1:+ FullyQualifiedErrorId:PositionalParameterNotFound,Set-NetIPAddress
2023/06/07 16:35:05 GCEMetadataScripts:windows-startup-script-ps1退出状态0
2023/06/07 16:35:05 GCEMetadataScripts:完成运行启动脚本。

对我来说让人头疼的是脚本在ISE中运行没有问题。任何帮助将不胜感激。我是PowerShell新手。

# -------在启动脚本开始之前等待5分钟
Start-Sleep -s 300

# -------获取网络接口索引并从接口中删除现有的DHCP IP地址
Set-NetIPInterface -InterfaceIndex (Get-NetAdapter).InterfaceIndex -Dhcp Disabled

# -------删除并添加新的静态IP地址
Set-NetIPAddress -InterfaceIndex (Get-NetAdapter).InterfaceIndex IPAddress "x.x.x.x" -DefaultGateway "x.x.x.x" -PrefixLength "x"

# -------将DNS服务器地址设置为GCP域控制器
Set-DNSClientServerAddress InterfaceIndex (Get-NetAdapter).InterfaceIndex ServerAddresses ("x.x.x.x","x.x.x.x")

# -------将DNS连接后缀设置为etmcorad.com
Set-DnsClient InterfaceIndex (Get-NetAdapter).InterfaceIndex -ConnectionSpecificSuffix "etmcorad.com"
英文:

Having a rough time getting a PS script to run during windows startup. I keep getting the below error message.
2023/06/07 16:32:45 GCEMetadataScripts: Starting startup scripts (version 20230403.00).
2023/06/07 16:32:45 GCEMetadataScripts: Found windows-startup-script-ps1 in metadata.
2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1: Set-NetIPAddress : A positional parameter cannot be found that accepts argument '4'.
2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1: At C:\Windows\TEMP\metadata-scripts3738309189\windows-startup-script-ps1.ps1:8 char:1

2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1: + Set-NetIPAddress -InterfaceIndex (Get-NetAdapter).InterfaceIndex �?"I ...
2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1: + CategoryInfo : InvalidArgument: (:) [Set-NetIPAddress], ParameterBindingException
2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1: + FullyQualifiedErrorId : PositionalParameterNotFound,Set-NetIPAddress
2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1:
2023/06/07 16:35:05 GCEMetadataScripts: windows-startup-script-ps1 exit status 0
2023/06/07 16:35:05 GCEMetadataScripts: Finished running startup scripts.

The head scratcher for me is that the script runs in ISE without issue. Any help would be greatly appreciate. I'm new to PowerShell.

#-------Wait 5 minutes before startup script begins
Start-Sleep -s 300

#-------Get Network Interface Index and Remove Existing DHCP IP Address from Interface
Set-NetIPInterface -InterfaceIndex (Get-NetAdapter).InterfaceIndex -Dhcp Disabled

#-------Remove Add New Static IP Address
Set-NetIPAddress -InterfaceIndex (Get-NetAdapter).InterfaceIndex –IPAddress "x.x.x.x" -DefaultGateway "x.x.x.x" -PrefixLength "x" 

#-------Set DNS Server Addresses to GCP Domain Controllers 

Set-DNSClientServerAddress –InterfaceIndex (Get-NetAdapter).InterfaceIndex –ServerAddresses ("x.x.x.x","x.x.x.x")

#-------Set DNS Connection Suffix to etmcorad.com

Set-DnsClient –InterfaceIndex (Get-NetAdapter).InterfaceIndex -ConnectionSpecificSuffix "etmcorad.com"

答案1

得分: 1

根据评论中的观察,格式化错误输出以便理解。

对于基本故障排除,请逐个检查您正在运行的命令的有效性。不要在ISE中执行。请以SYSTEM用户身份(可能是在启动时执行脚本的帐户)在PS控制台中执行(使用PSEXEC以System身份运行Powershell)。或者,只需将脚本减少为一行,以查看启动时会发生什么。

似乎在第一行出错了。在以SYSTEM身份运行的PS控制台中,这个命令能工作吗?在GCP环境中,这是否是Windows服务器的有效命令?

此博客文章和链接的脚本似乎适用于在通过DCHP分配保留地址之后,将网络适配器设置为使用静态地址。也许可以阅读指南并查看脚本的构建方式。再次,逐个运行每个命令,以找出错误。

英文:

As observed in the comment, format that error output so it makes sense.

For basic troubleshooting, check the validity of the commands you're running, one by one. Not in ISE. Do it as the SYSTEM user (presumably - whatever account is executing the script on startup) in a PS console (using PSEXEC to run Powershell as System). Or, just reduce the script to one line to see what happens on startup.

It seems to be bombing out on the first line. Does that work in a PS console running as system? Is it a valid command for a Windows server in the GCP environment?

This blog article and linked script seems to apply that the network adapter can be set to use a static address, after a reserved address is assigned via DCHP. Perhaps read the guide and look at how the script is constructed. And once again, run each command one-at-a-time to zero in on the error.

huangapple
  • 本文由 发表于 2023年6月8日 05:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定