Quantcast
Channel: PowerShell.org » All Posts
Viewing all 13067 articles
Browse latest View live

Copying file using CIM_logical file in PS

$
0
0

Hi Guys,

I am new to CIM technology and its usability in Powershell. I am trying to copy all txt files from FolderA to FolderB. Both files are located on the root of C.

The script runs successfully with return code 2 with is "Access denied". I have checked the folder permissions to which full control is granted to administrator. Also I am running PowerShell ISE as admin. Following is my scripting

$query= "SELECT * FROM CIM_LogicalFile WHERE Path= '\\folderA\\' AND Extension = 'txt' "
$obj = Get-WmiObject -ComputerName "localhost" -Query $query
$obj.Copy("C:\\folderB")

Your help is highly appreciated.

Regards
Faraz Amjad


Reply To: Copying file using CIM_logical file in PS

$
0
0

I'm pretty sure you have to specify the complete destination path and file name.

Software Inventory via Registry Error

$
0
0

Hi all.

I am trying to create an exportable report of software installed on systems, either servers or pc's via the registry with the following line of commands and I am not sure where I am failing. I have researched online, and through forums and from what I've read it should be working. Any guidance would be appreciated. Thank you.

"invoke-command -scriptblock {Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate} -ComputerName (Get-content test1.txt) | export-csv software.csv" – is what I am using and below is the error I get.

Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the
command again.
At line:1 char:202
+ … -ComputerName (Get-content test1.txt) | Export-Csv software.csv
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Setting up Audit rules with PS

$
0
0

Where I work we have several security policies, which include the auditing of certain folders. Below is a script I'm working on, the $folders variable will contain many system folders such as "c:\windows", c:\windows\system32 etc.

Can anyone forsee an issue with the below script? (It should just run through and put the everyone group in the failure audit with all boxes checked)

Also, is there a way to display the folder's auditing policy? I tried ($folder | get-acl).getauditrules but it just displays the method properties.

I would like to also be able to pull a report that would say like

folder, audit policies
c:\test, failure – everyone – full control

Thanks for the assistance in advance!

$folders = "C:\test"
$User = "Everyone"
$Rules = "FullControl"
$InheritType = "None"
$AuditType = "Failure"
$hostn = hostname

write-host "$hostn"
foreach($folder in $folders)
{
$ACL = new-object System.Security.AccessControl.DirectorySecurity
$AccessRule = New-Object System.Security.AccessControl.FileSystemAuditRule($user,$Rules,$InheritType,"None",$AuditType)
$ACL.SetAuditRule($AccessRule)
$ACL | Set-Acl $Folder
write-host "Setting Audit Rules on $folder"

}

Reply To: Setting up Audit rules with PS

$
0
0

Thanks! Looks pretty good. I'm not too worried about over writting any audting on these folders for the 'everyone group', as it will always be non-existant or less than 'fullcontrol' which is required to be compliant to our security doc.

Thanks again sir!

Reply To: replacing text in a txt file.

$
0
0

Tim,

Thank you for your response, I may have added some complexity to my example that was not needed. The final product will actually not have a – in the file name like this:

$old = '"enabled_labs_experiments": [ ],'
$new = '"enabled_labs_experiments": [ "disable-direct-write" ],'
(Get-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State") | ForEach-Object {$_ -replace $old, $new} | Set-Content "C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\Local State"

Th actual value that we want to replace is the following line:
"enabled_labs_experiments": [ ],

and replace it with:

"enabled_labs_experiments": [ "disable-direct-write" ],

in the text file (actually a config file) called:
Local State

that is located in the directory:
C:\Users\Jesse\AppData\Local\Google\Chrome\User Data\

Reply To: The WBEM Server limits have been exceeded?

$
0
0

Gunna have to build another 2012 R2 server to test on, unfortunately everything here is standardized on 2008 R2 still, but I'll give it a shot. Thanks for the help!

Reply To: Copying file using CIM_logical file in PS

$
0
0

You can simplify your WMI call a bit
$files = Get-WmiObject -Class CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"

$file.Copy($newfile)

}

As Don said you need to give the full path to the new file

You can also do this

$files = Get-WmiObject -Class CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"

Invoke-WmiMethod -InputObject $file -Name Copy -ArgumentList $newfile

}

OR

use the new CIM cmdlets

$files = Get-CimInstance -ClassName CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"

Invoke-CimMethod -InputObject $file -MethodName Copy -Arguments @{Filename = $newfile}

}


Reply To: The WBEM Server limits have been exceeded?

$
0
0

Its the number of files that is causing the problem. The SubItems property returns a list of every file in the directory structure. Gunna have to deploy them as Archive or maybe Chocolatey packages or something.

Reply To: replacing text in a txt file.

$
0
0

OK, second time lucky! :-)

I think it's because the -replace operator is interpreting the string as containing a special regex character, the opening square bracket.

When I change it to use the Replace method of a string, it worked OK.

ForEach-Object {$_.replace($old, $new)}

Reply To: Invoke-DSCPull runs twice

$
0
0

Why not use DSC alone from the get-go – there are a bunch of features and ecosystem components missing that start feeling very painful as the scope of what you have under configuration management grows. Automated testing of resource and generated configurations, notifications between resources, dynamically determining settings at configuration time, and more are all challenges in DSC that other CM tools can help with (especially those that also embrace DSC).

Reply To: replacing text in a txt file.

$
0
0

You got it Tim.

Thank you so much for your help!

Regex question

$
0
0

Hello All,

I am not really good at regex. How do I filter out only those strings with ALL UPPERCASE (strictly no lowercase, numbers are allowed)?

In this below example, i would like to filter only "GB8639NKH6"

Here is what I tried.

PS [22:54:27] D:\> $A = "GB8639NKH6","86232a62","VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80"
 
PS [22:54:59] D:\> $a
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80
 
PS [22:55:02] D:\> $A | where {$_ -cmatch "[A-Z]"}
GB8639NKH6
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80
 
PS [22:55:33] D:\> $A | where {$_ -cmatch "[^a-z]"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80
 
PS [22:55:57] D:\> $A | where {$_ -cmatch "[^a-z]*"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80
 
PS [22:56:09] D:\> $A | where {$_ -cmatch "[^a-z]*$"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80
 
PS [22:57:25] D:\> $A | where {$_ -cmatch "[A-Z]*$"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80

Reply To: Install Software from network path using Package

$
0
0

DSC runs as the local machine. on a domain then "Domain Comptuers" needs access to the SMB share and the NTFS permissions. (I dont think URI's work) on a workgroup, the "Everyone" group should work.

Reply To: Copying file using CIM_logical file in PS

$
0
0

THANKS A MILLION GUYS!!!!!!

Both of you are very right. I was missing file name from my script and after filling up the file name(s), the return code is 0 which is success :)

Regards


Dynamic resouce list possible to create Azure VMs?

$
0
0

Hi Folks,

I have a DSC question that we are stuck with. We are dealing with a script that will need to handle a flexible list of VMs (many different possible environment configurations possible) that we need to create on Azure. We are using DSC to create the machines. The issue you guys probably have run into at some point is that when you create machines on Azure, you actually implement a resource on “localhost”, so you only have one single mof file. When you need more than one machine, you need to something that is not pretty: repeat the resources and have reoccurring properties in the one localhost node such as ServiceName1 ServiceName2 etc.

It would be great however to be able to handle this dynamic and hence I was trying to get this to work:

param(
[Parameter(Mandatory,ValueFromPipeline)][string]$AllVMFileName,
[Parameter(Mandatory,ValueFromPipeline)][PSCredential]$CredentialsToSetInsideVM
)

Import-Module xAzure

Configuration cAeroAzureProvisioning
{

$AllVMs = get-content $AllVMFileName

Node $AllNodes.Where{$_.NodeName -eq 'localhost'}.NodeName
{
$index = 0
foreach ($vm in $AllVMs)
{
$index += 1
$xAzureSubscriptionResource = 'AzureSubscriptionResource{0}' -f $index
$xAzureStorageAccountResource = 'AzureStorageAccountResource{0}' -f $index
$xAzureServiceResource = 'AzureServiceResource{0}' -f $index
$xAzureVMResource = 'AzureVMResource${0}' -f $index

xAzureSubscription $xAzureSubscriptionResource
{
Ensure = 'Present'
AzureSubscriptionName = $Node.AzureSubscriptionName
AzurePublishSettingsFile = $Node.PublishingSettingsFile
}

xAzureStorageAccount $xAzureStorageAccountResource
{
Ensure = 'Present'
AffinityGroup = $vm.AffinityGroup
StorageAccountName = $vm.StorageAccountName
}

xAzureService $xAzureServiceResource
{
Ensure = 'Present'
AffinityGroup = $vm.AffinityGroup
ServiceName = $vm.ServiceName
}

xAzureVM $xAzureVMResource
{
Ensure = 'Present'
ImageName = $Node.ImageName
Name = $vm.Name
ServiceName = $vm.ServiceName
StorageAccountName = $vm.StorageAccountName
Credential = $CredentialsToSetInsideVM
InstanceSize = $vm.InstanceSize
Windows = $true
}
}
}
}

The script starts looping and I can see that it would actually be creating a very long mof file with all machines in it, but it fails complaining that I have duplicate resource names.

So somehow it does not recognize my dynamically generated resource names. I need to say that I am a PowerShell beginner, so It could be something simple, or I am trying something that is not possible.

Any Ideas?

Reply To: Copying file using CIM_logical file in PS

$
0
0

Guys,

Can I change the destination to network shared folder i.e. move or copy the folder to network shared folder. I have tried this with following script, the script runs successfully but return code is 9 i.e. invalid object.

$query= "SELECT * FROM CIM_LogicalFile WHERE Path= '\\folderA\\' AND Extension = 'txt' "

$files = Get-CimInstance -Query $query -ComputerName "localhost"

foreach($file in $files)
{
$newfile = "\\Server\\$($file.FileName).$($file.Extension)"
Invoke-CimMethod -InputObject $file -MethodName Copy -Arguments @{Filename = $newfile}

}

Appreciated

Faraz Amjad

Reply To: Regex question

$
0
0
$a | where { $_ -cmatch '^[A-Z0-9]+$' }

^ indicates start of line
$ indicates end of line
[A-Z0-9] indicates a pattern of [A-Z or 0-9]
+ indicates the preceding pattern must match any number of characters from 1 to infinity

* on the other hand indicates any number of characters from 0 to infinity, so using * would also match an empty string. + ensures there's at least one character match.

Reply To: Regex question

$
0
0

And to match a specific number of characters, instead of + you should use

{num} to indicate a specific amount of characters that must be matched, or
{num1,num2} to indicate a range

So ^[A-Z0-9]{8}$ would match any string that is exactly 8 characters long and contain only A-Z or 0-9.

Reply To: Output to Excel

$
0
0
Get-Service | Export-Csv -UseCulture -NoTypeInformation services.csv

This'll open up just fine in Excel. Granted it doesn't have colors, but it can run on any machine, even without Excel installed.

To get a list of services from different machines, you could do

foreach($computer in $computers) { 
    Get-Service -ComputerName $computer | Add-Member -NotePropertyName Computer -NotePropertyValue $computer -PassThru | Export-Csv -Append -UseCulture -NoTypeInformation services.csv 
}
Viewing all 13067 articles
Browse latest View live