How to get general link field properties in Sitecore Powershell Extensions - powershell

Using SPE (Sitecore Powershell Extensions) I need an Item's general link type field "Url" property, but have been running into problems converting the type.
I've tried to convert Item property to Linkfield object like this:
[Sitecore.Data.Fields.LinkField]$field = $myolditem["Email"]
Output:
Error converting string to Linkfield
How do I convert the string value of the field to a Linkfield type using SPE?

Try using
[Sitecore.Data.Fields.LinkField]$field = $myolditem.Fields["Email"]
$Url = $field.Url
You should get the Url of the Link

There are different types of Links : Internal, External, Media Links etc.
In case if you want to retrieve URLs irrespective of link types, please use the below lines..
[Sitecore.Xml.Xsl.LinkUrl]$fieldLink = New-Object -TypeName 'Sitecore.Xml.Xsl.LinkUrl'
$Url = $fieldLink.GetUrl($myolditem, "Email")

Related

Powershell - changing value of returned SQL data variable fails

In Powershell, I am doing a SQL query for a single row of data. Lets say $data for example.
The response from the query a System.Data.DataSet type. Within it, there is a tables property that has the data I need.
$data.Tables
ServerName : Server15
SamAccount : Admin-Server15
LastPWDReset : 1/15/2019 12:00:00 AM
LastPWDResetAttempt :
I don't intend to write this data back out of anything. Instead, I want to display it, and convert the empty "LastPWDResetAttemp" to "NONE" where it is blank.
I thought it would be done like this:
$data.Tables.lastPWDResetAttempt = "None"
but that gives me an error The property 'lastPWDResetAttempt' cannot be found on this object. Verify that the property exists and can be set.
I can't help but think I am missing some conversion from "Dataset" to "String".
I've tried to-string but in doing so, I ended up with just a string of data and not the headings. Nothing I could update, or easily use to build my eventual table.
My work around:
$webdata = "" | select ServerName,SamAccount,LastPWDReset,LastPWDResetAttempt
$webdata.ServerName = $data.tables.servername
$webdata.SamAccount = $data.tables.samaccount
$webdata.LastPWDReset = $data.tables.LastPWDReset
$webdata.LastPWDResetAttempt = $data.tables.LastPWDResetAttempt
$webdata.LastPWDResetAttempt = "Never"
works. I just can't believe there isn't an easier way, nor do I understand why I can view a list of the data, just to not then be able to set it.
I think it is because Tables is an array and does not have the property LastPWDResetAttempt.
You can try the following instead:
$data.Tables[0].Rows[0].LastPWDResetAttempt = "None"
Also I think your workaround, though it may contain more lines of code, is actually a better solution though.

How to use underscore while formatting a string from Sitecore in PowerShell

I am getting two field values from an item in Sitecore. When I concatenate using string format, it's not showing the values. Instead, it shows only an underscore.
I have tried the following formats. None of them are working.
"Name" = "{$record.FirstName}_{$record.LastName}";
"Name" = [string]::Format("{0}{1}",{$record.FirstName},{$LastName});
Note: The values are coming fine from Sitecore. The issue is only with formatting.
You've to either change to:
$Name = "$($record.FirstName)_$($record.LastName)";
or to:
$Name = "{0}_{1}" -f $record.FirstName, $record.LastName;
Hope that helps.

Using [array] in a class based DSC resource

I have been creating a few class based custom DSC resources and I am running into the following issue:
Whenever I try to use an array as input for my resource, I get an error.
For example
DscProperty(Mandatory)]
[array]$Products
Would result in the following error when I try to create a MOF file using my resource:
Write-NodeMOFFile : Invalid MOF definition for node 'nodename':
Exceptioncalling "ValidateInstanceText" with "1" argument(s):
"Convert property 'Products' value from type 'STRING[]' to type 'INSTANCE' failed.
The input object for $Products would be (for example):
$Products = #("Windows server 2012", "Windows SQL Server", "Windows 8.1")
I honestly have no idea why the Write-NodeMOFFile function would try to convert the array (it should not need converting, right?) and even if it needed to be converted - why would it convert an array from STRING[] to INSTANCE?
Anyone has a clue as to why this happens?
The only way I got it to work was by creating a long string from my array of strings and then seperating them within the resource.
declare array like this :
[string[]]$product="ddd","dq","dqq"

How to modify internalname in Sharepoint 2010 list field

I need to change the internalname for a sharepoint 2010 list item field. For some reason, our migration software renamed it during 2007->2010 migration and this field is referenced by other processes so we need the internalname back to the original. This field exists in over 200 lists in the migrated site so we need a means to do this programatically - powershell preferred.
$newInternalName = "yourInternalFieldName"
$displayName = "oldDisplayName"
$SPWebApp = Get-SPWebApplication "http://yourwebapp"
foreach (SPList $currList in $SPWebApp.Lists)
{
foreach (SPField $fld in $currList.Fields) #you could potentially use a different command here to get the field more efficiently
{
if ($fld.Name == $displayName)
{
#The boolean in the parameter list is for required/non-required field $currList.Fields.Add($newInternalName,Microsoft.SharePoint.SPFieldType.Text,$False)
foreach (SPListItem $item in $currList.Items)
{
$item[$newInternalName] = $item[$displayName]
$item[$newInternalName].Title = $displayName #I'm assuming you want to keep the display name the same
$item.Update()
}
Break #since you've already fixed the column for this list no need to keep going through the fields
# optional delete unwanted column
# $currList.Fields.Delete($displayName)
# $currList.Update()
}
}
}
As far as I know, you cannot change the internal name of a field once it has been created. Here I create a new field with the correct internal name and copy over the values. I don't have access to a server with Powershell today so I wasn't able to test this, but this should be very close to what you need. You may need to tweak it a bit based on what type of field you're dealing with, if you want to use a different Add function, and/or you want to delete the old field.
The enumeration to select the type of the field in the Add function is defined here:
http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.spfieldtype(v=office.14).aspx
There are a couple of overloads for the Add function so if the one I used doesn't work for you, you might wanna use one of these others
http://msdn.microsoft.com/en-us/library/office/aa540133(v=office.14).aspx

How can I use PowerShell to set the default document in IIS?

I'm configuring a web site using PowerShell and I want to set the default document. How do I do this?
This is one way:
$metabasePath = "IIS://Localhost/W3SVC"
$iisNumber = "12345"
$site = new-object
System.DirectoryServices.DirectoryEntry("$metabasePath/$iisNumber/root")
$site.psbase.Properties["DefaultDoc"].Value =
"newdefdoc.htm," + $site.psbase.Properties["DefaultDoc"].Value
$site.psbase.CommitChanges()
The value returned in $site.psbase.Properties["DefaultDoc"].Value is a comma separated list of documents so you may need to re-jig the order to suit your case. The example above just adds a new default document (newdefdoc.htm) to the top of the list.