Through a PowerShell script, I'm trying to interact with SCCM 2012 R2 Task Sequences. A lot of sites refer to using something similar to the following:
$tsenv = new-object -ComObject microsoft.sms.tsenvironment
First, if there is a better/different way, I'm totally open to it. But for this way, whenever I run that line, regardless of how I loaded the ConfigurationManager module (e.g. launch PowerShell from SCCM console, load the ConfigurationManager.psd1 from an x86 or x64 PowerShell console), it always returns the error below. I've also tried this on lab SCCM 2012 R2 environments and 2 separate production environments:
"New-Object : Retrieving the COM class factory for component with CLSID ... failed due to the following error: 80040154 Class not registered"
I'm coming up short searching for answers online, so hoping someone here is able to use that ComObject in PowerShell with SCCM 2012 R2 or if I'm just out of luck.
FYI,
In addition to having an active task sequence, you should also make sure that you are running from x64 process when creating the object when running on 64-bit OS, o/w the creation will fail with the 'Class not registered' error as if there is no TS running.
In my environment I have a single server that has access to pretty much my entire network. That server is running Windows 2008 r2, and I have upgraded Powershell to version 4.0. The question I have is this... Can I run cmdlets from that machine on other machines that are version 4 specific?
For instance, when I am using Powershell, even though it is version 4, it doesn't give me an intellisense autocomplete for "Get-Volume" like it would on a 2012 r2 machine. I understand that it won't run on that machine because the infrastructure won't allow for it, but what about a 2012 r2 machine remotely?
I am looking to run batch scripts from there for various purposes.
First, this is probably a ServerFault-question as it's related to server-administration.
PowerShell 4.0 installed on 2008 R2 can't run 2012 cmdlets on a 2012 R2-machine like Get-Volume -ComputerName My2012Server, because the cmdlets doesn't exist on your 2008 R2 machine. However, you should be able to invoke the cmdlet on the 2012 R2-server, like:
Invoke-Command -ComputerName My2012Server -Scriptblock { Get-Volume }
Be aware that you would not get autocomplete support when writing it as the commands and help files aren't installed on your 2008 R2-server
Import-PSSession is also a possibility if your gonna run the commands interactively. For a script I would probably still use Invoke-Command.
Get-Volume in your example is available only on Windows Server 2012 and above. So, it won't auto-complete on a 2008 R2 system. You can use PowerShell implict remoting.
Using implict remoting, you can import all cmdlets from a remote system into a local session and use them as if they are available on the local system.
I found a technet article to set the display resolution using powershell and Windows Server Core Cmdlets. http://technet.microsoft.com/en-us/library/jj603036.aspx
However when I try to run the cmd PS C:\> Set-DisplayResolution -Width 1920 -Height 1200 I get an error saying the Set-DisplayResoluton is a unknown cmd. I know this cmd is for administering server core functionlaity but can it be used in Windows 8.1. And if so, how do I load the cmdlet in Powershell?
Are you trying to run this on Windows 8.1? It looks like the command might only be available on Windows Server 2012 R2 Server Core. I understand that the article says otherwise, but I just checked on my Windows 8.1 computers, and do not have the command in the session.
Get-Command -Name *resolu*;
The situation. On a Windows 7 SP1 machine, I have updated with Windows6.1-KB2819745-x64-MultiPkg.msu. Furthermore, in PowerShell $PSVersionTable now reports ‘PSVersion 4.0’.
At present, my conclusion is that many PowerShell 4 cmdlets such Test-NetConnection, will only work on Windows 8.1. However, I was wondering if there was a work-around whereby I could import PowerShell 4 modules on my Windows 7 machine.
You cannot. They rely on the underlying features of the newer OS (8.0 or 8.1) and cannot be ported back to Windows 7 . The alternative is to write your own functions / modules to replicate the new cmdlets using .NET framework methods.
For instance, the Get-FileHash cmdlet is a one-liner in PowerShell 4.0, but to replicate in 2.0 we have to use .NET.
PowerShell v4
Get-FileHash -Algorithm SHA1 "C:\Windows\explorer.exe"
PowerShell v2
$SHA1 = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
$file = [System.IO.File]::Open("C:\Windows\explorer.exe",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
[System.BitConverter]::ToString($SHA1.ComputeHash($file)) -replace "-",""
$file.Close()
At least Test-NetConnection can be ported back to Windows 7. Just copy folders NetTCPIP, DnsClient, and NetSecurity from the supported Windows machine with the same PowerShell version (Windows 8.1, Windows 10, etc). Folder - C:\Windows\System32\WindowsPowerShell\v1.0\Modules. Then Import-Module -Name C:\Windows\System32\WindowsPowerShell\v1.0\Modules\NetTCPIP -Verbose
Alternatively, you can import a module from a remote machine (say win2012):
$rsession = New-PSSession -ComputerName win2012
Import-Module NetTCPIP -PSSession $rsession
I have had the same problem on my Windows 7 x64 and both solutions worked for me as of PowerShell 5.1.
Adding to Anton Krouglov's answer. PowerShell modules are cross-platform compatible. So a module copied from Windows Server 2012 R2 x64 can be imported to Windows 7 x86, and even if you are running as standard user without rights to copy them to C:\Windows\System32\WindowsPowerShell\v1.0\Modules you can copy it to any local folder, and run.
Assuming you copied the NetTCPIP, DnsClient, and NetSecurity modules from a Windows Server 2012 or higher machine, and save them to a folder you can import them using
Get-ChildItem -Directory .\psmodules | foreach { Import-Module -Name $_.FullName -Verbose}
Test-NetConnection -InformationLevel "Detailed"
As far as I know, Windows Server 2008 R2/Windows 7 simply doesn't have the counters that the .NET methods use to implement get-netstuff.
A new PowerShell version can implement hash compare, etc. since this is not related to anything, just a piece of code. But if you want to use, for example, Get-NetTCPConnection there is nothing to show.
I see several responses which assert portability, and my testing confirms their assertions:
Import all of the required modules, either from file, or via a PSSession to a host which has the required modules.
The architecture of PowerShell Console (x86 or x64) you run will determine which module architecture you import.
For those who are:
still unable to make this work
AND
do need a reliable TCP test, but may not need everything else provided by Test-NetConnection
AND
Need it all to work even on PowerShell v2.0
You may wish to try this.
# Create a TCP Client using .Net & attempt connection to target
$TCPClient = New-Object .net.sockets.tcpclient("[IP address or hostname]",[port])
# At the above point you may see familiar-looking Windows error messages in
# red text. (You may want to use "try/catch" if you intend to loop).
# Otherwise, check your new object's status to see if it worked
$TCPClient.Connected
# The response is either "True" or False"
# Now to avoid leaving idle connections, run:
$TCPClient.Close()
Naturally, it should be possible to create a loop which tests multiple connections, and outputs the results by selecting the properties of the $TCPClient.
My initial testing shows you would want to Select these properties
The address you tested
$TCPClient.Client.RemoteEndPoint.Address.IPAddressToString
The port you tested
$TCPClient.Client.RemoteEndPoint.Port
The result
$TCPClient.Connected
HIH
While PowerShell 4.0 is available on Windows 7, as Knuckle-Dragger states certain features rely on newer operating system functionality. Unfortunately Test-NetConnection is not available in Windows 7 as stated in the documentation.
Test-Connection, which is present, is basically ping. Test-NetConnection offers much more functionality, allowing a choice of things such as TCP ports, protocols, route tracing, and information levels.
There is a Send-Ping script available from the ScriptCenter in the TechNet gallery, but I think this is only really useful if you are stuck on PowerShell 3.0 for some reason.
I can only assume you installed the wrong package. Make sure you download the proper package from here.
Below you will see in running Windows 7 Service Pack 1 with PowerShell 4 using Test-Connection and Get-FileHash:
We are porting Unix scripts to PowerShell. I am totally new to PowerShell and have problem in finding replacement for the at command in Unix.
We have a script that is scheduled using the at command:
SEQ_NO=12; export SEQ_NO
A=R2.13575180; export A
B=R4.13575180.DAT; export B
chmod 777 R4.13575180.DAT
echo "sh R4.13575180.DAT > M.reclog " |
at now + 2 minutes
R4.13575180.DAT would contain PowerShell commands inside. I was able to find replacements for other commands. I need to find a replacement for the at command. I did go through certain links, but I am still not clear. Could you suggest a replacement?
Since you're new to PowerShell, here is some general advice:
Make sure you are running the latest operating system possible, to get the most recent features (eg. Windows 8.1)
Make sure you have installed the latest Microsoft .NET Framework version (eg. Microsoft .NET Framework 4.5.1)
The Microsoft .NET Framework 4.5.1 is already included with Windows 8.1
Make sure you are running the latest PowerShell (aka. Windows Management Framework (WMF) Core) version
The latest version of WMF is 4.0
The WMF / PowerShell 4.0 package is built into Windows 8.1 (Windows Server 2012 R2) and is available for Windows 7 (Server 2008 R2)
WMF 4.0 is not available for Windows 8.0
IMPORTANT: The ScheduledTasks PowerShell module is available only on Windows 8.1 with the Microsoft .NET Framework 4.5.1 and PowerShell version 4.0.
You're most likely looking for the PowerShell module named ScheduledTasks, if you want to schedule something to run in the future.
To find out what commands are in the ScheduledTasks module, use this command:
Get-Command -Module ScheduledTasks;
To be more specific in answer to your question, here is an example of how to create a ScheduledTask:
# 1. Create a trigger
$Trigger = New-ScheduledTaskTrigger -At '2013-12-30 12:00:00 AM' -Once;
# 2. Create an action
$Action = New-ScheduledTaskAction -Execute powershell.exe -Argument '-Command Write-Host -Object "hi";'
# 3. Create the scheduled task
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Description PowerShell;
# 4. Register the ScheduledTask
Register-ScheduledTask -InputObject $Task -TaskName PowerShell;
# 5. [Optional] View / confirm the ScheduledTask object
Get-ScheduledTask -TaskName PowerShell;
Since you're running Windows 7 / PowerShell 2.0 you might want to grab the TaskScheduler module from PowerShellPack.
That will get you cmdlets for managing scheduled tasks.