我开发了一个模块“XPTO.psm1”,我创建了我的自定义类:
class XPTO {
[string]$baseurl
[string]$fileToken
$session = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
在我的主脚本 SCRIPT.ps1 中,我使用:
#! /usr/bin/pwsh
Using module '/root/scripts/XPTO.psm1'
然后当我运行命令“pwsh”并调用脚本时,一切正常:
[root@XXXXXXXX scripts]# pwsh
PowerShell 6.2.3
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /root/scripts> ./SCRIPT.ps1
True
Collecting metrics
10/15/19 4:41:57 PM - Starting collecting metrics from 762 applications
10/15/19 4:41:57 PM - Started Multithread
但是如果我在 pwsh 之外运行它不起作用,我会收到这个错误:
[root@XXXXXXXXX scripts]# pwsh ./SCRIPT.ps1
At /root/scripts/XPTO.psm1:198 char:17
+ ... $session = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [Microsoft.PowerShell.Commands.WebRequestSession].
+ CategoryInfo : InvalidOperation: (/root/scripts/scrip\u2026ts/XPTO.psm1:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound