I am using PowerShell 2.0 and using the following code to join domain:
$domain = "test.org.hk"
$password = "password" | ConvertTo-SecureString -AsPlainText -Force
$username = "$domain\Administrator"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential
After joining the domain, I use the following code to rename computer:
(Get-WmiObject Win32_ComputerSystem).Rename($nameStr)
I find that I cannot change computer name.
But, if I don't join the domain, I can use this code to change computer name.
Therefore, how to rename computer after join the domain in PowerShell v2.0?
Related
I am preparing VM deployment, and on VMs I need to run some powershell command lets. I need to install Windows Server role, and initialize new ssd disk. I am working on Azure cloud, Terraform deployment has been tested, works as expected, VM is deployed with all required configuration, but facing some problems during multiline powershell commands. I use terraform resource azurerm_virtual_machine_extension, it works when I am trying to install/use one block of code, for example for Windows server role installation. I don't want to use storage account and container and .ps1 script file uploaded to storage container, I need to use inline powershell code. My terraform clode below.
resource "azurerm_virtual_machine_extension" "vmext" {
name = "vmext"
virtual_machine_id = azurerm_windows_virtual_machine.app_vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
settings = <<SETTINGS
{
"commandToExecute": "powershell -ExecutionPolicy Unrestricted Install-windowsfeature -name RoleName -IncludeManagementTools; powershell -ExecutionPolicy Unrestricted Get-Disk | Where-Object -FilterScript {$_.PartitionStyle -eq 'RAW'} | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel 'disk2' -Confirm:$false"
}
SETTINGS
}
Try this:
resource "azurerm_virtual_machine_extension" "vmext" {
name = "vmext"
virtual_machine_id = azurerm_windows_virtual_machine.app_vm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
settings = <<SETTINGS
{
"commandToExecute": <<EOF
powershell -ExecutionPolicy Unrestricted Install-windowsfeature -name RoleName -IncludeManagementTools
powershell -ExecutionPolicy Unrestricted Get-Disk | Where-Object -FilterScript {$_.PartitionStyle -eq 'RAW'} | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel 'disk2' -Confirm:$false"
EOF
}
SETTINGS
}
Hope this helps!
I am adding 200+ hyperlinks to an excel file for easier access to sharepoint folders. Rather than manually pulling each one I am trying to bulk export the file paths from a sharepoint folder. Any ideas on how to do this?
We can use PnP PowerShell to achieve it.
#Config Variables
$SiteURL = "https://tenant.sharepoint.com/sites/lz"
$ListName = "DL0906"
$FolderRelativeURL= "/sites/lz/DL0910/Test"
$ExportFile="C:\temp\FileUrls.csv"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Get All Items from the Folder
$CAMLQuery = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef'/><Value Type='Text'>$FolderRelativeURL</Value></Eq></Where></Query></View>"
$FolderItems = Get-PnPListItem -List $ListName -Query $CAMLQuery
$FileCollection = #()
ForEach($Item in $FolderItems)
{
$ExportFileUrl = New-Object PSObject
$ExportFileUrl | Add-Member -MemberType NoteProperty -name "File URL" -value $Item["FileRef"]
$FileCollection += $ExportFileUrl
}
$FileCollection | Export-CSV $ExportFile -NoTypeInformation
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Refer to: SharePoint Online: Get All Files from a Folder using PowerShell
This is the error message when I am attempting to install a domain controller on the forest mysite.com.
00domaincontroller.ps1
Import-Module ActiveDirectory
Import-Module ServerManager
Enable-PSRemoting -force
$DCPromoFile =
#"
[DCINSTALL]
UserDomain="mysite.com"
UserName="Administrator"
Password="password1234"
ReplicaOrNewDomain="Replica"
ReplicaDomainDNSName="mysite.com"
DatabasePath="c:\Windows\NTDS"
LogPath="c:\Windows\NTDS"
SYSVOLPath="c:\Windows\SYSVOL"
InstallDNS="Yes"
CriticalReplicationOnly="No"
ConfirmGC="Yes"
SafeModeAdminPassword="password4321"
RebootOnCompletion="Yes"
"#
$DCPromoFile | out-file "c:\folders\dcpromo.ini" -force
I'm trying to register an OCX file on a remote machine using Powershell 2.0.
This doesn't doesn't work:
$LocalOCXPath = "C:\Windows\SysWOW64\dxapi.ocx"
Invoke-Command -ComputerName $ComputerName -ScriptBlock { "C:\windows\system32\Regsvr32.exe $args" } -argumentlist $LocalOCXPath
But this does:
Invoke-Command -ComputerName $ComputerName -ScriptBlock { & 'regsvr32.exe' 'C:\Windows\SysWOW64\dxapi.ocx' }
I really need to be able to pass the path in via a variable, but I don't think I can do that using option 2. And I have no idea why option 1 doesn't work. By doesn't work, I mean that powershell looks like it invokes the command properly, but when I look on the remote machine, the ocx file isn't registered.
I think you are overenginnering, try:
$LocalOCXPath = "C:\Windows\SysWOW64\dxapi.ocx"
Invoke-Command -ComputerName $ComputerName -ScriptBlock { & 'regsvr32.exe' $LocalOCXPath }
Invoke-Command runs the scriptblock in the context of the remote system so your $LocalOCXPath variable doesn't exist there. In PowerShell 3.0 you can force the variable to be seen in the remote context by prefixing it with $Using:, as in $Using:$LocalOCXPath.
I need to copy files to multiple computers from my computer with following specifications.
I need to provide username
I need to provide password also
while running it should not prompt again for password important
I used the following code but it asks for the password multiple times.
read-host -assecurestring | convertfrom-securestring | out-file e:\SSS\pass.txt
$password=get-content e:\SSS\pass.txt | convertto-securestring
$credential=new-object -typename System.Management.Automation.PSCredential -argumentlist KS\KS012\Administrator, $password
Script below should prompt for credentials and then prompt to ask for location of txt file which has line seperated list of PC's you want to deploy to. Then it will ask for the location of the file you want to copy and then ask for the destination with a preformated UNC c$ formatting.
You can adjust this to set the list of PCs, location of file to copy and desitnation to hardcoded.
Get-Credential domain\usermname
$PC = Read-Host "Location of PC List"
$FileLocation = Read-Host "Enter File Location"
$FileDestination = Read-Host "Enter File Destination"
Get-Content $PC | foreach {Copy-Item $FileLocation -Destination \\$_\c$\$FileDestination}
:)