Import Powerhsell DNS server module - powershell

I am deploying some virtual machines by script, and I would like to check if the FQNA that I adding to my script is already created in the DNS records.
I found the commands to do this, but I can't find what Powershell module I need to use these commands.
Found DNSserver and others, but this doesn't work and I am a bit confused which module should I import/install to be able to use Powershell DNS commands to use like get-dnsserverresourcerecord or Add-DnsServerResourceRecordA etc.
Thank You

I had this issue as well. This module is part of RSAT. If you only want the get-dnsserverresourcerecord or Add-DnsServerResourceRecordA, that is in the feature Rsat.Dns.Tools~~~~0.0.1.0.
From an elevated prompt in PowerShell:
Add-WindowsCapability -Online -Name Rsat.Dns.Tools~~~~0.0.1.0
If you want all of the RSAT features:
Get-WindowsCapability -Name RSAT* -Online | Add-WindowsCapability -Online
Verified in PowerShell 7.2.5
Found this information here:
https://mikefrobbins.com/2018/10/03/use-powershell-to-install-the-remote-server-administration-tools-rsat-on-windows-10-version-1809/

The cmdlets are in the DNSServer module.
https://learn.microsoft.com/en-us/powershell/module/dnsserver/?view=windowsserver2022-ps
get-dnsserverresourcerecord
https://learn.microsoft.com/en-us/powershell/module/dnsserver/get-dnsserverresourcerecord?view=windowsserver2022-ps
add-dnsserverresourcerecorda
https://learn.microsoft.com/en-us/powershell/module/dnsserver/add-dnsserverresourcerecorda?view=windowsserver2022-ps

Related

Updating SSL bindings for IIS with Powershell 7

I'm trying to update a PS5 script to PS7, mainly because the script does work that requires a PS Core module.
Part of the script involved updating IIS bindings to use a different SSL Certificate. The cert is in the store and ready to be used - I just need to change the thumbprint on the binding.
My PS5 script used Get-WebConfiguration to get the bindings and then just looped through, calling RebindSslCertificate on relevant bindings.
I've tried using Set-WebConfigurationProperty and Set-WebBinding; neither errors but neither actually updates the binding with IIS - example:
Set-WebConfigurationProperty -Name 'certificateHash' -Value $newCert.Thumbprint -PSPath "IIS:\\" `
-Filter "/system.applicationHost/sites/site/bindings/binding[#protocol='https'][#bindingInformation='*:443:hostname']" `
Could anyone help point me in the right direction for what I'm missing?
Thanks,
Mark.
P.S., Apologies if this is a repeat question but all I can find is old stuff that doesn't work or relates to "-Set-Item IIS:\SslBindings" Maybe there is someway to get the IIS drive working with remoting?
Ran into this on 9/10/2021 using Powershell 7.1.4.
As of date of writing, this is an open issue on github for PowerShell.
Link for reference: https://github.com/PowerShell/PowerShellModuleCoverage/issues/14
Issue is that PowerShell 7 is based on .NET Core and the PS module WebAdministrator is based on .NET Framework.
When you run
Import-Module WebAdministration
WARNING: Module WebAdministration is loaded in Windows PowerShell using WinPSCompatSession remoting session; please note that all input and output of commands from this module will be deserialized objects. If you want to load this module into PowerShell please use 'Import-Module -SkipEditionCheck' syntax.
Notice the mention of 'WinPSCompatSession' in the warning. If the module manifest doesn't indicate that the module is compatible with PowerShell Core, then it gets loaded via the Windows PowerShell Compatibility Feature.
It seems this module partially works in compatibility mode, however if you try to work with IIS:\ then you start getting errors.
Alternatively, if you run the parameter in the warning you get this.
Import-Module -SkipEditionCheck WebAdministration
Import-Module: Could not load type 'System.Management.Automation.PSSnapIn' from assembly 'System.Management.Automation, Version=7.1.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
A quick test in PowerShell 7.1.4 will show you that you can't access the IIS connector.
PS C:\Windows\System32> Import-Module WebAdministration
WARNING: Module WebAdministration is loaded in Windows PowerShell using WinPSCompatSession remoting session; please note that all input and output of commands from this module will be deserialized objects. If you want to load this module into PowerShell please use 'Import-Module -SkipEditionCheck' syntax.
PS C:\Windows\System32> cd IIS:\
Set-Location: Cannot find drive. A drive with the name 'IIS' does not exist.
However, if you open up PowerShell 6 you can do this no problem.
PS C:\WINDOWS\system32> Import-Module WebAdministration
PS C:\WINDOWS\system32> cd IIS:\
PS IIS:\> dir
Name
----
AppPools
Sites
SslBindings
My next step is trying to get this to work by loading the .NET assembly directly. Will update with the solution
[System.Reflection.Assembly]::LoadFrom("$env:systemroot\system32\inetsrv\Microsoft.Web.Administration.dll")

Get-Command -Module MicrosoftTeams does not include Get-TeamChannelUser cmdlet

So, I'm looking to do some powershell automation now that my company is clamoring for Microsoft Teams. I installed the module version 1.0.5 (currently the latest) and I'm seeing documentation on how to add users to team channels.
I get cmdlet not found errors when using those commandlets
Get-TeamChannelUser : The term 'get-teamchanneluser' is not recognized as the name of a cmdlet, function, script file...
Low and behold, get-command -module MicrosoftTeams returns no cmdlets with TeamChannelUser (Get, Remove or Set).
I've tried older versions of the module but I'm still seeing the same symptom. Is anyone else able to reproduce this? Any workarounds?
I haven't tested this specifically, but have you looked into the new (pre-release) Microsoft Graph PowerShell module? It includes a lot of "Teams" functions, but also of course more broad "Graph" capabilities, which is important (recall that Teams relies on SharePoint, Groups, etc.).
I had to manually install a test version 1.0.21, but now when I run the Get-Command, I see the ChannelUser cmdlets.
Thanks to Hilton Giesenow, I'm going to try his suggestion as well and will mark it as the answer if it works better and post an update.
I struggled to get this working. I ended up installing 1.1.3.
This is what worked for me:
PS C:\WINDOWS\system32> Install-Module -Name MicrosoftTeams -RequiredVersion 1.1.3-preview -AllowPrerelease -force -AllowClobber
PS C:\WINDOWS\system32> Import-Module microsoftteams
PS C:\WINDOWS\system32> get-command -Module microsoftteams

Installing Windows Updates via PSWindowsUpdate

I am trying to remotely make a domain-computer install its windows updates. This sounds like it should be quite easy, but I've been working on this for over 7 hours now and can't get it to work. I know you can do this via a GPO, but that doesn't give me enough control over the interval. I want our servers to install them and reboot monthly - a GPO can only be used to install and reboot weekly. Since our production works 24/7 I absolutely don't want the servers to reboot outside of the few hours downtime per month I am allowed for maintenance!
I have found several tutorials like this that use the Module PSWindowsUpdate, but these tutorials use an older version of that Module. They use a Function called Invoke-WUInstall which doesn't exist in the newest version. I have tried downgrading the module, but the packagesource doesn't provide versions older than 2.0.0.0
Also the project page doesn't provide a documentation - no examples - neither does it have a discussion or bugtracker. There is a discussion on the page of the original author, but he stopped working on it 2 years ago when it was still the old version.
I tried using Invoke-Command instead of Invoke-WUInstall, but Windows doesn't seem to allow remote update installation like that. PSWindowsUpdate apparently circumvents this problem by running the command as a scheduled task on the target machine, so looking at the output of Get-Command -Module PSWindowsUpdate, I thought I might need to use Invoke-WUJob instead and wrote this code:
Import-Module -Name PSWindowsUpdate
Enable-PSRemoting -Force
ForEach ($hostname in $args) {
Write-Output "Processing $hostname"
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $hostname -Concatenate -Force
# Install PSWindowsUpdate on target machine
Invoke-Command -computername $hostname -ScriptBlock {
PackageManagement\Get-PackageProvider -Name NuGet -Force
inmo PSWindowsUpdate -Force
}
# Install the Updates
Invoke-WUJob -ComputerName $hostname -Script {
ipmo PSWindowsUpdate;
Install-WindowsUpdate -install -AcceptAll -IgnoreReboot
} -Confirm:$false -RunNow
}
I run this as a user who has administrative rights on the target machine and the output looks fine, but it didn't do anything.
Does anyone have experience with that module? how do you do this properly in versions >= 2?
Well, i already did something like this and also faced this same problem.
That was my solution:
Create a powershell file to execute your commands. Place all your commands to install the updates there.
Copy this file to the remote server.
You can do something like this:
copy myfile.ps1 \\myserver\c$\temp\myfile.ps1;
Run a remote script to create a registry inside the RunOnce, and the set value with a command to run your script:
Set-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name '!InstallUpdates' -Value "c:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -File c:\temp\myfile.ps1"
Run another command remotelly to restart your server.
The server will reboot and then will execute your script locally.

Dynamically create a cmdlets/module in PowerShell

This is what I want to achieve and I can't find anything about this.
I want to create a remote module proxy, for a module that is available on a remote server.
I know how to work with remoting but I want something that creates cleaner script files.
To give an example. If I want to execute MyCmdlet from module MyModule on a remote computer I would do this
$block={
# Invoke the cmdlet from a module named MyCmdlet
MyCmdlet -Parameter1 -Parameter2
}
Invoke-Command -ComputerName "" -ScriptBlock $block
But I would like to land into something like this
Import-Module MyModuleRemote
MyCmdlet -ComputerName "" -Parameter1 -Parameter2
Please noticed that MyModule is not installed on my client machine.
I could re-write the module with Invoke-Command wrapper for each cmdlet but that is not the purpose. What I would like to do is remot-ify the MyModule by creating an proxy equal proxy per cmdlet and parameter. Even the Get-Help should work at least for the parameter composition.
I have a couple of ideas but I'm not sure if it is even possible.
Create a powershell module e.g. PSRemotify that will probe the module on the remote server and generate the code.
If I chose to write files to the file system then this should be possible, if I could do reflection on the cmdlets.
If I don't want to save files then I need to do everything in memory. Can I write a cmdlet's body in memory? Can I generate a string and import its embedded cmdlet?
Create a script that does 1.2.
My preference would be option 1.2. Very clean and without leaving any traces on the file system.
Any ideas? Has anybody tried something like already?
Conclusion after my investigation and answer from #Persistent13:
PowerShell offer this feature out of the box. it is known as IMPLICIT REMOTING. Before #Persistent13's answer I took the wrong part because I think it is interesting to share my experience, I've blogged about it.
import and use module from a remote server
It sounds like what your looking for is implicit remoting and is fairly simple to set up.
Please note I've taken the instructions for this from here.
To create an implicit session you would:
PS C:\> $foo = New-PSSession -ComputerName DC1
PS C:\> Import-Module -PSSession $foo -Name ActiveDirectory
PS C:\> Get-ADUser
The above would open a PowerShell session on the computer DC1, import the ActiveDirectory module on the remote computer, and the command is run against the remote computer while it appears to be executed locally.
It is also possible the prefix the implicitly imported modules in case a local module would conflict with the remote one.
PS C:\> $foo = New-PSSession -ComputerName DC1
PS C:\> Import-Module -PSSession $foo -Name ActiveDirectory -Prefix DC1
PS C:\> Get-DC1ADUser
The advantage of this is there is no need to remot-ify your module and so long as it is present on the remote computer and PowerShell remoting is allowed.
However one caveat of this method is that the type of the object returned will change.
Deserialized.Microsoft.ActiveDirectory.Management.ADUser when implicit remoting is used.
Microsoft.ActiveDirectory.Management.ADUser when run locally.
When I raised the question back then, I didn't know the concept of implicit remoting.
In practice you import a module using Import-Module but you also specify a session to a remote server with the -Session parameter.
For example to import Microsoft.PowerShell.Management from server $target="SERVER" execute this
$session=New-PSSession -ComputerName $target
Import-Module Microsoft.PowerShell.Management -Session $session
With this a proxy module is generated in the current session that offers the same cmdlets as the one specified. The proxy cmdlets automatically push the parameters to the remote session $session and capture its output. With Microsoft.PowerShell.Management implicitly imported from SERVER the Get-Service returns the services from SERVER.
You can really see the implementation of the proxy cmdlets by executing
$module=Get-Module Microsoft.PowerShell.Management
$module.Definition
It's at least interesting.

IIS 7.5 powershell module usage issues

Has anyone managed to use this module with success, i'm running 32bit Windows 7, where i have opened an administrator shell using run as administrator, i have imported the WebAdministration module and then attempted to use the commands with some issues, have provided two examples here
Websites
I created a web site with the following command
new-website -name testsite -port 80 -hostheader testsite -physicalpath c:\temp
Then i attempted to get the sites details using the command
get-website -name testsite
but it always returns all sites, seems to ignore the -name parameter. Only way i can get the site is using a filter
get-website | ? { $_.name -eq 'testsite' } | get-member
When i use appcmd it works as expected using the following command
C:\> C:\Windows\System32\inetsrv\appcmd.exe list site testsite
AppPools
When i try to list the apppools using the following command
dir iis:\apppools
i get the following error
Get-ChildItem : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
Yet when i use appcmd as follows i get all the apppools as expected without any error
C:\Windows\System32\inetsrv\appcmd.exe list apppool
Has anyone successfully managed to use the WebAdministration module ?
Thanks in advance
Pat
If you are already running PowerShell as an administrator and have Windows UAC disabled and you are still seeing this error, make sure WAS (Windows Process Activation Service) is running. You can stop W3SVC but PowerShell will definitely give the same head-scratching "access denied" error if WAS is stopped.
You need to make sure that the PowerShell window is running elevated, try right-clicking in the Start Menu and using the "Run as administrator" option to ensure it runs elevated.
Regarding Get-Website ignoring the -Name parameter, this appears to be a bug according to this forum post. The workaround is to use Get-Item
$website = "Default Web Site"
Get-Item "IIS:\sites\$website"
Be sure to use double quotes, variables are not expanded when single quotes are used.
Regarding browsing the application pools I suspect this is a permissions issue as CarlosAg mentioned. Make sure that you are running PowerShell with elevated privileges. I typically launch PowerShell by right-clicking on it and selecting "Run as administrator".