DATOS Y METODOLOGÍA
3.5 ESTADÍSTICA DESCRIPTIVA
You can now run these commands, just as if they were locally installed, and can even access their help (provided the server has had Update-Help run so that it has a copy of the help locally). The only caveat is the one that applies to all results in Remoting: The results of your commands won’t have any methods attached to them, because the results will have been through the serialization/deserialization process.
These “imported” commands will exist as long as your session to the remote machine is open and available. Once it’s closed, the commands will vanish. If you want to make these commands always available to you, save the remote session information to a module using the Export-PSSession cmdlet.
There are a few ways you might want to use this. First, take your current session and export everything to a module:
PS C:\> Export-PSSession -Session $q -OutputModule QuarkAll
The session $q is to the computer named Quark. This command will create a module called QuarkAll under $home\Documents\WindowsPowerShell\Modules:
PS C:\> Get-Module -ListAvailable QuarkAll
ModuleType Name ExportedCommands --- ---- --- Manifest QuarkAll {}
Later, you can import this module as you would with implicit Remoting. Because the imported cmdlet names may conflict, add a prefix:
PS C:\> Import-Module QuarkAll -Prefix Q
The first time you try to run one of the commands, PowerShell dynamically creates the necessary session and establishes a remote connection:
PS C:\> Get-Qsmbshare
Creating a new session for implicit Remoting of "Get-SmbShare" command...
If you check sessions, you should see a new one created for this module:
PS C:\> Get-PSSession | select * State : Opened ComputerName : quark ConfigurationName : Microsoft.PowerShell InstanceId : 662484ed-d350-4b76-a146-865a8d43f603 Id : 2
Name : Session for implicit Remoting module at
C:\Users\Jeff\Documents\WindowsPowerShell\Modules\ QuarkAll\QuarkAll.psm1
Availability : Available ApplicationPrivateData : {PSVersionTable}
Runspace : System.Management.Automation.RemoteRunspace
If you remove the module, the session is also automatically removed.
You can also create a limited module by only exporting the commands you want. First, create a session:
Then, create a new module exporting only the Get cmdlets:
PS C:\> Export-PSSession -Session $q -OutputModule QuarkGet –CommandName ➥ Get* [CA] -CommandType cmdlet
When you import the module, the only commands you can run remotely on Quark are the Get cmdlets:
PS C:\> Import-Module QuarkGet -Prefix Q PS C:\> Get-Command -module QuarkGet
CommandType Name Definition --- ---- --- Function Get-QAppLockerFileInformation ... Function Get-QAppLockerPolicy ... Function Get-QAppxProvisionedPackage ... Function Get-QAutoEnrollmentPolicy ... Function Get-QBitsTransfer ... ...
One thing we should point out is that when you export a session, any commands with names that might conflict on your local computer are skipped unless you use the -AllowClobber parameter. In the examples with Quark, you’re connecting from a computer running PowerShell 2.0 to one running PowerShell 4.0, or 3.0, and thus are able to use the cmdlets of the later versions of PowerShell just as if they were installed locally:
PS C:\> get-qciminstance win32_operatingsystem | Select ➥ CSName,BuildNumber,Version
Creating a new session for implicit Remoting of "Get-CimInstance" command... CSName BuildNumber Version
--- --- --- QUARK 8250 6.2.8250
Implicit Remoting is an incredibly powerful technique—and a necessity for working with remote Exchange servers—that lets you take advantage of modules, snap-ins, and tools that you may not have installed locally. If you find yourself needing these tools often, take the time to export a session to a module; then you’ll be ready for anything.
10.12 Standard troubleshooting methodology
Troubleshooting can be difficult, especially with Remoting because there are so many layers in which something can go wrong. We strongly recommend that you read, learn, and inwardly digest the help file about_Remote_Troubleshooting. It contains a lot of useful information that will improve your knowledge of Remoting and enable you to troubleshoot problems. When you have to diagnose problems with Remoting, we recommend that you follow these four steps:
1 Test Remoting with its default configuration. If you’ve tinkered with it, undo your changes and start from scratch.
2 Start by attempting to connect from the initiating machine to the target machine by using something other than Remoting but that’s still security-sensitive. For
83 Summary
example, use Windows Explorer to open the remote machine’s C$ shared folder. If that doesn’t work, you have broader security issues. Make a note of whether you need to provide alternate credentials—if you do, Remoting will need them as well.
3 Install a Telnet client on the initiating machine (a simple command-line client, like the Windows native one, will do). Attempt to connect to the HTTP WinRM listener by running telnetmachine_name:5985. You should get a blank screen, and Ctrl-C will end the session. If this doesn’t work, there’s a basic connectivity problem (such as a blocked port) you need to resolve.
4 Use Test-WSMan, using an alternate credential if necessary. Make sure that you’re using the machine’s real name as it appears in Active Directory or that you’ve taken one of the other approaches (TrustedHosts plus a credential, or SSL plus a credential). If that doesn’t work, you have a problem in the WSMAN configuration.
Walking through these four steps, in this order, can help you pinpoint at least the gen- eral cause of most problems.
10.13 Summary
Remoting was the most eagerly awaited feature in PowerShell v2. It moved Power- Shell’s capabilities up by several levels. You can gain remote access to systems through a number of cmdlets that have a –ComputerName parameter or through the WSMAN- based Remoting technology.
Once you’ve mastered the material in this chapter, you’ll be able to administer all the machines in your environment from the comfort of your own workstation.
PowerShell in Depth, Second Edition is the go-to reference for administrators working with Windows PowerShell. Every major technique, technology, and tactic is care- fully explained and demonstrated, providing a hands- on guide to almost everything an admin would do in the shell. Written by three experienced authors and PowerShell MVPs, this is the PowerShell book you'll keep next to your monitor—not on your bookshelf!
A Windows admin using PowerShell every day may not have the time to search the net every time he or she hits a snag. Wouldn't it be great to have a team of sea- soned PowerShell experts ready to answer even the toughest questions? That's what you get with this book.
PowerShell in Depth, Second Edition is the go-to reference for administrators working with Windows PowerShell. Every major technique, technology, and tactic is carefully explained and demonstrated, providing a hands-on guide to almost everything an admin would do in the shell. Written by PowerShell MVPs Don Jones, Jeffrey Hicks, and Richard Siddaway, each valuable technique was developed and thoroughly tested, so you'll be able to consistently write production-quality, maintainable scripts while saving hours of time and effort.
What's inside:
Automating tasks
Packaging and deploying scripts
Introduction to Desired State Configuration
PowerShell security
Covers PowerShell version 3 and later
S
QL Server is a common member of Windows environments, and you may find yourself having to administer this technology. You can work with SQL Server with PowerShell in a number of ways—scripting, cmdlets and the SQL Server Provider. This chapter introduces the provider and gives you a number of scripts you can use in your environment to perform common tasks.