using System.Management.Automation;
PowerShell ps = PowerShell.Create();
//ps.AddCommand("Get-Process"); // if uncommented, works.
ps.AddCommand("Enable-NetFirewallRule").AddParameter("DisplayName","firewall_rule_1");
ps.Invoke();
(This is the continuation of the my previous question, since it seems I can't follow up Muhammad's previous answer.
(See previous question "System.Management.Automation : Not Found : 'Enable-NetFirewallRule'" )
c-sharpcorner.com/forums/systemmanagementautomation-not-found-sharp39enablenetfirewallrulesharp39 )
Thank you very much to Muhammad, I appreciate it.
I have followed your recommendations but it seems that the "Import-Module NetSecurity" command doesn't work, even in admin mode.
I guess I should see the "Microsoft.Windows.Firewall.Commands.dll" file in my app directory :
"C:\c#\fw\bin\Debug\net8.0\" .... but I see nothing...
Also, the file "fw.deps.json" (in that same directory) doesn't give any reference to the NetSecurity module.
I worked hard on this problem and the only improvement/advance I made is to copy the "NetSecurity" module Directory..
From : c:\Windows\system32\WindowsPowerShell\v1.0\Modules\NetSecurity
To : C:\Users\myName\Documents\PowerShell\Modules
the result : "'Enable-NetFirewallRule' command was found BUT AuthorizationManager check failed..", see below :
> dotnet run
--------------------------------------------------------------
> dotnet run
Unhandled exception. System.Management.Automation.CommandNotFoundException: The 'Enable-NetFirewallRule' command was found in the module 'NetSecurity', but the module could not be loaded due to the following error: [The following error occurred while loading the extended type data file:
, C:\Users\myName\Documents\PowerShell\Modules\NetSecurity\NetSecurity.types.ps1xml: The file was skipped because of the following validation exception: AuthorizationManager check failed..
]
For more information, run 'Import-Module NetSecurity'.
---> System.Management.Automation.CmdletInvocationException: The following error occurred while loading the extended type data file:
, C:\Users\myName\Documents\PowerShell\Modules\NetSecurity\NetSecurity.types.ps1xml: The file was skipped because of the following validation exception: AuthorizationManager check failed..
---> System.Management.Automation.RuntimeException: The following error occurred while loading the extended type data file:
, C:\Users\myName\Documents\PowerShell\Modules\NetSecurity\NetSecurity.types.ps1xml: The file was skipped because of the following validation exception: AuthorizationManager check failed..
at System.Management.Automation.Runspaces.InitialSessionState.ThrowTypeOrFormatErrors(String resourceString, String errorMsg, String errorId)
at System.Management.Automation.Runspaces.InitialSessionState.UpdateTypes(ExecutionContext context, Boolean updateOnly)
at System.Management.Automation.Runspaces.InitialSessionState.Bind_UpdateTypes(ExecutionContext context, Boolean updateOnly)
at System.Management.Automation.Runspaces.InitialSessionState.<>c__DisplayClass129_0.b__0()
at System.Management.Automation.Runspaces.InitialSessionState.Bind(ExecutionContext context, Boolean updateOnly, PSModuleInfo module, Boolean noClobber, Boolean local, Boolean setLocation)
at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String moduleManifestPath, ExternalScriptInfo manifestScriptInfo, Hashtable data, Hashtable localizedData, ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion, Nullable`1 requiredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)
--- End of inner exception stack trace ---
...
I then tried to launch this powershell 7.4.1 in admin mode :
> c:\users\myName\.dotnet\tools\.store\powershell\7.4.1\powershell\7.4.1\tools\net8.0\any\win\pwsh.exe
then cd to C:\c#\fw\bin\Debug\net8.0\
then launch the ".\fw.exe" (still admin mode) ===> same "AuthorizationManager check failed" error as above.
--------------------------------------------------------------
FYI, in my Visual Code terminal, I entered the following commands :
> $env:PSModulePath
C:\Users\myName\Documents\PowerShell\Modules;
C:\Program Files\PowerShell\Modules;
c:\users\myName\.dotnet\tools\.store\powershell\7.4.1\powershell\7.4.1\tools\net8.0\any\win\Modules;
C:\Program Files\WindowsPowerShell\Modules;
c:\Windows\system32\WindowsPowerShell\v1.0\Modules;
!!! the "NetSecurity" module is ONLY located in the LAST path ("c:\Windows\system32\WindowsPowerShell\v1.0\Modules\NetSecurity" )
For some reason, if I don't copy this "NetSecurity" module directory to "C:\Users\myName\Documents\PowerShell\Modules",
then the 'Enable-NetFirewallRule' command isn't found! (as if the "NetSecurity" module directory wouldn't be in the last $PSModulePath !)
However the "Get-Module -ListAvailable" command sees it ! (see below)
--------------------------------------------------------------
> Get-ExecutionPolicy
Bypass
--------------------------------------------------------------
the Powershell version used by VSCode :
c:\users\myName\.dotnet\tools\.store\powershell\7.4.1\powershell\7.4.1\tools\net8.0\any\win\pwsh.exe
> $PSVersionTable
Name Value
---- -----
PSVersion 7.4.1
PSEdition Core
GitCommitId 7.4.1
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
--------------------------------------------------------------
the Powershell version used by my Windows 11 (click left on windows logo (taskbar), click right on "Windows PowerShell (Admin)"
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.22621.2506
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22621.2506
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
--------------------------------------------------------------
> Get-Module -ListAvailable
Directory: C:\Users\myName\Documents\PowerShell\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Manifest 2.0.0.0 NetSecurity (!!! THE COPY I MADE) Core,Desk {Get-DAPolicyChange, New-NetIPsecAuthProposal, New-NetIPse…
Directory: C:\users\myName\.dotnet\tools\.store\powershell\7.4.1\powershell\7.4.1\tools\net8.0\any\win\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Manifest 7.0.0.0 CimCmdlets Core {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance,…
Manifest 1.2.5 Microsoft.PowerShell.Archive Desk {Compress-Archive, Expand-Archive}
Manifest 7.0.0.0 Microsoft.PowerShell.Diagnostics Core {Get-WinEvent, New-WinEvent, Get-Counter}
Manifest 7.0.0.0 Microsoft.PowerShell.Host Core {Start-Transcript, Stop-Transcript}
Manifest 7.0.0.0 Microsoft.PowerShell.Management Core {Add-Content, Clear-Content, Get-Clipboard, Set-Clipboard…}
Binary 1.0.1 Microsoft.PowerShell.PSResourceGet Core,Desk {Find-PSResource, Get-InstalledPSResource, Get-PSResourceR…
Manifest 7.0.0.0 Microsoft.PowerShell.Security Core {Get-Acl, Set-Acl, Get-PfxCertificate, Get-Credential…}
Manifest 7.0.0.0 Microsoft.PowerShell.Utility Core {Export-Alias, Get-Alias, Import-Alias, New-Alias…}
Manifest 7.0.0.0 Microsoft.WSMan.Management Core {Disable-WSManCredSSP, Enable-WSManCredSSP, Get-WSManCredS…
Script 1.4.8.1 PackageManagement Desk {Find-Package, Get-Package, Get-PackageProvider, Get-Packa…
Script 2.2.5 PowerShellGet Desk {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap…
Script 7.0.0.0 PSDiagnostics Core {Disable-PSTrace, Disable-PSWSManCombinedTrace, Disable-WS…
Script 2.3.4 PSReadLine Desk {Get-PSReadLineKeyHandler, Set-PSReadLineKeyHandler, Remov…
Binary 2.0.3 ThreadJob Desk Start-ThreadJob
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Script 1.0.1 Microsoft.PowerShell.Operation.Val… Desk {Get-OperationValidation, Invoke-OperationValidation}
Binary 1.0.0.1 PackageManagement Desk {Find-Package, Get-Package, Get-PackageProvider, Get-Packa…
Script 3.4.0 Pester Desk {Describe, Context, It, Should…}
Script 1.0.0.1 PowerShellGet Desk {Install-Module, Find-Module, Save-Module, Update-Module…}
Script 2.0.0 PSReadLine Desk {Get-PSReadLineKeyHandler, Set-PSReadLineKeyHandler, Remov…
Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Manifest 1.0.0.0 AppBackgroundTask Core,Desk {Disable-AppBackgroundTaskDiagnosticLog, Enable-AppBackgro…
Script 1.0.0.0 AssignedAccess Core,Desk {Clear-AssignedAccess, Get-AssignedAccess, Set-AssignedAcc…
Manifest 1.0.0.0 BitLocker Core,Desk {Unlock-BitLocker, Suspend-BitLocker, Resume-BitLocker, Re…
Script 2.0.0.0 BitsTransfer Core,Desk {Add-BitsFile, Complete-BitsTransfer, Get-BitsTransfer, Re…
Manifest 1.0.0.0 BranchCache Core,Desk {Add-BCDataCacheExtension, Clear-BCCache, Disable-BC, Disa…
Manifest 1.0 ConfigDefenderPerformance Core,Desk {New-MpPerformanceRecording, Get-MpPerformanceReport}
Manifest 1.0 DefenderPerformance Core,Desk {New-MpPerformanceRecording, Get-MpPerformanceReport}
Manifest 1.0.3.0 DeliveryOptimization Core,Desk {Get-DeliveryOptimizationLog, Get-DeliveryOptimizationLogA…
Manifest 1.0.0.0 DirectAccessClientComponents Core,Desk {Disable-DAManualEntryPointSelection, Enable-DAManualEntry…
Script 3.0 Dism Core,Desk {Add-AppProvisionedSharedPackageContainer, Add-AppxProvisi…
Manifest 1.0.0.0 DnsClient Core,Desk {Resolve-DnsName, Clear-DnsClientCache, Get-DnsClient, Get…
Manifest 1.0.0.0 EventTracingManagement Core,Desk {Start-EtwTraceSession, New-EtwTraceSession, Get-EtwTraceS…
Script 2022.3.14… Get-NetView Core,Desk Get-NetView
Manifest 2.1.0.0 International Core,Desk {Copy-UserInternationalSettingsToSystem, Get-WinDefaultInp…
Manifest 1.0.0.0 Kds Core,Desk {Add-KdsRootKey, Get-KdsRootKey, Test-KdsRootKey, Set-KdsC…
Manifest 1.0 LanguagePackManagement Core,Desk {Install-Language, Uninstall-Language, Get-InstalledLangua…
Script 1.0.0.0 LAPS Core,Desk {Find-LapsADExtendedRights, Get-LapsADPassword, Invoke-Lap…
Manifest 1.0.0.0 Microsoft.PowerShell.LocalAccounts Core,Desk {Add-LocalGroupMember, Disable-LocalUser, Enable-LocalUser…
Manifest 1.0.0 Microsoft.Windows.Bcd.Cmdlets Core,Desk {Copy-BcdEntry, Disable-BcdElementBootDebug, Disable-BcdEl…
Manifest 1.0 MMAgent Core,Desk {Disable-MMAgent, Enable-MMAgent, Set-MMAgent, Get-MMAgent…
Manifest 2.0.0.0 NetAdapter Core,Desk {Disable-NetAdapter, Disable-NetAdapterBinding, Disable-Ne…
Manifest 2.0.0.0 NetConnection Core,Desk {Get-NetConnectionProfile, Set-NetConnectionProfile}
Manifest 1.0.0.0 NetEventPacketCapture Core,Desk {New-NetEventSession, Remove-NetEventSession, Get-NetEvent…
Manifest 2.0.0.0 NetLbfo Core,Desk {Add-NetLbfoTeamMember, Add-NetLbfoTeamNic, Get-NetLbfoTea…
Manifest 1.0.0.0 NetNat Core,Desk {Get-NetNat, Get-NetNatExternalAddress, Get-NetNatStaticMa…
Manifest 2.0.0.0 NetQos Core,Desk {Get-NetQosPolicy, Set-NetQosPolicy, Remove-NetQosPolicy, …
Manifest 2.0.0.0 NetSecurity Core,Desk {Get-DAPolicyChange, New-NetIPsecAuthProposal, New-NetIPse…
Manifest 1.0.0.0 NetSwitchTeam Core,Desk {New-NetSwitchTeam, Remove-NetSwitchTeam, Get-NetSwitchTea…
Manifest 1.0.0.0 NetTCPIP Core,Desk {Get-NetIPAddress, Get-NetIPInterface, Get-NetIPv4Protocol…
Manifest 1.0.0.0 NetworkConnectivityStatus Core,Desk {Get-DAConnectionStatus, Get-NCSIPolicyConfiguration, Rese…
Manifest 1.0.0.0 NetworkSwitchManager Core,Desk {Disable-NetworkSwitchEthernetPort, Enable-NetworkSwitchEt…
Manifest 1.0.0.0 NetworkTransition Core,Desk {Add-NetIPHttpsCertBinding, Disable-NetDnsTransitionConfig…
Manifest 1.0.0.0 PcsvDevice Core,Desk {Get-PcsvDevice, Start-PcsvDevice, Stop-PcsvDevice, Restar…
Manifest 1.0.0.0 PKI Core,Desk {Add-CertificateEnrollmentPolicyServer, Export-Certificate…
Manifest 1.0.0.0 PnpDevice Core,Desk {Get-PnpDevice, Get-PnpDeviceProperty, Enable-PnpDevice, D…
Manifest 1.1 PrintManagement Core,Desk {Add-Printer, Add-PrinterDriver, Add-PrinterPort, Get-Prin…
Binary 1.0.12 ProcessMitigations Core,Desk {Get-ProcessMitigation, Set-ProcessMitigation, ConvertTo-P…
Script 3.0 Provisioning Core,Desk {Install-ProvisioningPackage, Export-ProvisioningPackage, …
Manifest 1.0.0.0 ScheduledTasks Core,Desk {Get-ScheduledTask, Set-ScheduledTask, Register-ScheduledT…
Manifest 2.0.0.0 SecureBoot Core,Desk {Confirm-SecureBootUEFI, Set-SecureBootUEFI, Get-SecureBoo…
Manifest 2.0.0.0 SmbShare Core,Desk {Get-SmbShare, Remove-SmbShare, Set-SmbShare, Block-SmbSha…
Manifest 2.0.0.0 SmbWitness Core,Desk {Get-SmbWitnessClient, Move-SmbWitnessClient, gsmbw, msmbw…
Manifest 1.0.0.1 StartLayout Core,Desk {Export-StartLayout, Import-StartLayout, Export-StartLayou…
Manifest 2.0.0.0 Storage Core,Desk {Add-InitiatorIdToMaskingSet, Add-PartitionAccessPath, Add…
Manifest 2.0.0.0 TLS Core,Desk {New-TlsSessionTicketKey, Enable-TlsSessionTicketKey, Disa…
Manifest 1.0.0.0 TroubleshootingPack Core,Desk {Get-TroubleshootingPack, Invoke-TroubleshootingPack}
Manifest 2.0.0.0 TrustedPlatformModule Core,Desk {Get-Tpm, Initialize-Tpm, Clear-Tpm, Unblock-Tpm…}
Binary 2.1.639.0 UEV Core,Desk
Manifest 2.0.0.0 VpnClient Core,Desk {Add-VpnConnection, Set-VpnConnection, Remove-VpnConnectio…
Manifest 1.0.0.0 Wdac Core,Desk {Get-OdbcDriver, Set-OdbcDriver, Get-OdbcDsn, Add-OdbcDsn…}
Manifest 2.0.0.0 Whea Core,Desk {Get-WheaMemoryPolicy, Set-WheaMemoryPolicy}
Manifest 1.0.0.0 WindowsDeveloperLicense Core,Desk {Get-WindowsDeveloperLicense, Unregister-WindowsDeveloperL…
Script 1.0 WindowsErrorReporting Core,Desk {Enable-WindowsErrorReporting, Disable-WindowsErrorReporti…
Manifest 1.0.0.0 WindowsSearch Core,Desk {Get-WindowsSearchSetting, Set-WindowsSearchSetting}
Manifest 1.0.0.0 WindowsUpdate Core,Desk Get-WindowsUpdateLog
Tuhin PaulPosted Mar 10, 2024, 3:06 PM
You've made some progress in troubleshooting your issue with the Enable-NetFirewallRule command but are still encountering the "AuthorizationManager check failed" error. Copying NetSecurity Module: You mentioned that you copied the NetSecurity module directory from c:\Windows\system32\WindowsPowerShell\v1.0\Modules\NetSecurity to C:\Users\myName\Documents\PowerShell\Modules. This allows the command Enable-NetFirewallRule to be found. Despite finding the command, you're encountering an error related to the AuthorizationManager check when trying to execute it.
You've provided the versions of PowerShell you're using. One is PowerShell 7.4.1 and the other is PowerShell 5.1.
Few suggestions:
Check that the execution policy allows running scripts. You can check this by running Get-ExecutionPolicy in PowerShell. If it's not set to allow, you can change it using Set-ExecutionPolicy. Make sure you're running PowerShell as an administrator. Right-click on PowerShell and select "Run as Administrator" to check elevated privileges.
Confirm that the NetSecurity module you've copied is intact and properly loaded. Sometimes copying modules might cause issues if they're not copied correctly or if there are missing dependencies. If possible, try updating PowerShell to the latest version. Sometimes, updating can resolve compatibility issues and bugs. There might be Group Policies in place that restrict the execution of certain commands. Check if there are any Group Policies affecting PowerShell execution. check that all dependencies required by the NetSecurity module are available and properly configured.